Skip to content

Commit fc2e635

Browse files
committed
Lookup methods work on configuration classes as well
Issue: SPR-15316
1 parent 8b74150 commit fc2e635

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ private static class BeanFactoryAwareMethodInterceptor implements MethodIntercep
270270

271271
@Override
272272
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
273-
Field field = obj.getClass().getDeclaredField(BEAN_FACTORY_FIELD);
273+
Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
274274
Assert.state(field != null, "Unable to find generated BeanFactory field");
275275
field.set(obj, args[0]);
276276

277-
// Does the actual (non-CGLIB) superclass actually implement BeanFactoryAware?
277+
// Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
278278
// If so, call its setBeanFactory() method. If not, just exit.
279-
if (BeanFactoryAware.class.isAssignableFrom(obj.getClass().getSuperclass())) {
279+
if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
280280
return proxy.invokeSuper(obj, args);
281281
}
282282
return null;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4040
import org.springframework.beans.factory.annotation.Autowired;
4141
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
42+
import org.springframework.beans.factory.annotation.Lookup;
4243
import org.springframework.beans.factory.annotation.Qualifier;
4344
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
4445
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -779,6 +780,14 @@ public void testCollectionInjectionFromSameConfigurationClass() {
779780
assertSame(ctx.getBean(TestBean.class), bean.testBeans.get(0));
780781
}
781782

783+
@Test
784+
public void testBeanLookupFromSameConfigurationClass() {
785+
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanLookupConfiguration.class);
786+
BeanLookupConfiguration bean = ctx.getBean(BeanLookupConfiguration.class);
787+
assertNotNull(bean.getTestBean());
788+
assertSame(ctx.getBean(TestBean.class), bean.getTestBean());
789+
}
790+
782791

783792
// -------------------------------------------------------------------------
784793

@@ -1448,4 +1457,16 @@ public TestBean thing() {
14481457
}
14491458
}
14501459

1460+
@Configuration
1461+
static abstract class BeanLookupConfiguration {
1462+
1463+
@Bean
1464+
public TestBean thing() {
1465+
return new TestBean();
1466+
}
1467+
1468+
@Lookup
1469+
public abstract TestBean getTestBean();
1470+
}
1471+
14511472
}

0 commit comments

Comments
 (0)