Browse Source

Properly detect importing class metadata for lite configuration class

Closes gh-22920
pull/25019/head
Juergen Hoeller 6 years ago
parent
commit
fff3813d75
  1. 2
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java
  2. 72
      spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java

2
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

@ -460,7 +460,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -460,7 +460,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
public Object postProcessBeforeInitialization(Object bean, String beanName) {
if (bean instanceof ImportAware) {
ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
AnnotationMetadata importingClass = ir.getImportingClassFor(bean.getClass().getSuperclass().getName());
AnnotationMetadata importingClass = ir.getImportingClassFor(ClassUtils.getUserClass(bean).getName());
if (importingClass != null) {
((ImportAware) bean).setImportMetadata(importingClass);
}

72
spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java

@ -82,6 +82,22 @@ public class ImportAwareTests { @@ -82,6 +82,22 @@ public class ImportAwareTests {
assertThat(foo, is("xyz"));
}
@Test
public void directlyAnnotatedWithImportLite() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ImportingConfigLite.class);
ctx.refresh();
assertNotNull(ctx.getBean("importedConfigBean"));
ImportedConfigLite importAwareConfig = ctx.getBean(ImportedConfigLite.class);
AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
assertThat("import metadata was not injected", importMetadata, notNullValue());
assertThat(importMetadata.getClassName(), is(ImportingConfigLite.class.getName()));
AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
Class<?>[] importedClasses = importAttribs.getClassArray("value");
assertThat(importedClasses[0].getName(), is(ImportedConfigLite.class.getName()));
}
@Test
public void importRegistrar() {
ImportedRegistrar.called = false;
@ -135,7 +151,7 @@ public class ImportAwareTests { @@ -135,7 +151,7 @@ public class ImportAwareTests {
@Configuration
@EnableImportedConfig(foo="xyz")
@EnableImportedConfig(foo = "xyz")
static class IndirectlyImportingConfig {
}
@ -180,6 +196,34 @@ public class ImportAwareTests { @@ -180,6 +196,34 @@ public class ImportAwareTests {
}
@Configuration
@Import(ImportedConfigLite.class)
static class ImportingConfigLite {
}
@Configuration(proxyBeanMethods = false)
static class ImportedConfigLite implements ImportAware {
AnnotationMetadata importMetadata;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.importMetadata = importMetadata;
}
@Bean
public BPP importedConfigBean() {
return new BPP();
}
@Bean
public AsyncAnnotationBeanPostProcessor asyncBPP() {
return new AsyncAnnotationBeanPostProcessor();
}
}
static class BPP implements BeanPostProcessor, BeanFactoryAware {
@Override
@ -274,6 +318,32 @@ public class ImportAwareTests { @@ -274,6 +318,32 @@ public class ImportAwareTests {
}
@Import(LiteConfiguration.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableLiteConfiguration {
String value() default "";
}
@Configuration(proxyBeanMethods = false)
public static class LiteConfiguration implements ImportAware {
private AnnotationMetadata importMetadata;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.importMetadata = importMetadata;
}
@Bean
public MetadataHolder holder() {
return new MetadataHolder(this.importMetadata);
}
}
public static class MetadataHolder {
private final AnnotationMetadata importMetadata;

Loading…
Cancel
Save