Skip to content

Commit fff3813

Browse files
committed
Properly detect importing class metadata for lite configuration class
Closes gh-22920
1 parent 00a5106 commit fff3813

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public PropertyValues postProcessProperties(@Nullable PropertyValues pvs, Object
460460
public Object postProcessBeforeInitialization(Object bean, String beanName) {
461461
if (bean instanceof ImportAware) {
462462
ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
463-
AnnotationMetadata importingClass = ir.getImportingClassFor(bean.getClass().getSuperclass().getName());
463+
AnnotationMetadata importingClass = ir.getImportingClassFor(ClassUtils.getUserClass(bean).getName());
464464
if (importingClass != null) {
465465
((ImportAware) bean).setImportMetadata(importingClass);
466466
}

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

+71-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public void indirectlyAnnotatedWithImport() {
8282
assertThat(foo, is("xyz"));
8383
}
8484

85+
@Test
86+
public void directlyAnnotatedWithImportLite() {
87+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
88+
ctx.register(ImportingConfigLite.class);
89+
ctx.refresh();
90+
assertNotNull(ctx.getBean("importedConfigBean"));
91+
92+
ImportedConfigLite importAwareConfig = ctx.getBean(ImportedConfigLite.class);
93+
AnnotationMetadata importMetadata = importAwareConfig.importMetadata;
94+
assertThat("import metadata was not injected", importMetadata, notNullValue());
95+
assertThat(importMetadata.getClassName(), is(ImportingConfigLite.class.getName()));
96+
AnnotationAttributes importAttribs = AnnotationConfigUtils.attributesFor(importMetadata, Import.class);
97+
Class<?>[] importedClasses = importAttribs.getClassArray("value");
98+
assertThat(importedClasses[0].getName(), is(ImportedConfigLite.class.getName()));
99+
}
100+
85101
@Test
86102
public void importRegistrar() {
87103
ImportedRegistrar.called = false;
@@ -135,7 +151,7 @@ static class ImportingConfig {
135151

136152

137153
@Configuration
138-
@EnableImportedConfig(foo="xyz")
154+
@EnableImportedConfig(foo = "xyz")
139155
static class IndirectlyImportingConfig {
140156
}
141157

@@ -180,6 +196,34 @@ public String otherImportedConfigBean() {
180196
}
181197

182198

199+
@Configuration
200+
@Import(ImportedConfigLite.class)
201+
static class ImportingConfigLite {
202+
}
203+
204+
205+
@Configuration(proxyBeanMethods = false)
206+
static class ImportedConfigLite implements ImportAware {
207+
208+
AnnotationMetadata importMetadata;
209+
210+
@Override
211+
public void setImportMetadata(AnnotationMetadata importMetadata) {
212+
this.importMetadata = importMetadata;
213+
}
214+
215+
@Bean
216+
public BPP importedConfigBean() {
217+
return new BPP();
218+
}
219+
220+
@Bean
221+
public AsyncAnnotationBeanPostProcessor asyncBPP() {
222+
return new AsyncAnnotationBeanPostProcessor();
223+
}
224+
}
225+
226+
183227
static class BPP implements BeanPostProcessor, BeanFactoryAware {
184228

185229
@Override
@@ -274,6 +318,32 @@ public MetadataHolder holder() {
274318
}
275319

276320

321+
@Import(LiteConfiguration.class)
322+
@Target(ElementType.TYPE)
323+
@Retention(RetentionPolicy.RUNTIME)
324+
public @interface EnableLiteConfiguration {
325+
326+
String value() default "";
327+
}
328+
329+
330+
@Configuration(proxyBeanMethods = false)
331+
public static class LiteConfiguration implements ImportAware {
332+
333+
private AnnotationMetadata importMetadata;
334+
335+
@Override
336+
public void setImportMetadata(AnnotationMetadata importMetadata) {
337+
this.importMetadata = importMetadata;
338+
}
339+
340+
@Bean
341+
public MetadataHolder holder() {
342+
return new MetadataHolder(this.importMetadata);
343+
}
344+
}
345+
346+
277347
public static class MetadataHolder {
278348

279349
private final AnnotationMetadata importMetadata;

0 commit comments

Comments
 (0)