diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index a078a71649..4baaa4f94d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -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); } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java index 7599fa0d8a..6259dfa828 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java @@ -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 { @Configuration - @EnableImportedConfig(foo="xyz") + @EnableImportedConfig(foo = "xyz") static class IndirectlyImportingConfig { } @@ -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 { } + @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;