Skip to content

Commit 99ea429

Browse files
committed
Added specific unit test for scoped-proxy target not being an autowire candidate
Issue: SPR-11548
1 parent 5308b3e commit 99ea429

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.Before;
2020
import org.junit.Test;
2121

22+
import org.springframework.aop.scope.ScopedObject;
2223
import org.springframework.aop.scope.ScopedProxyUtils;
2324
import org.springframework.beans.factory.FactoryBean;
2425
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,7 @@
3031
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3132
import org.springframework.beans.factory.support.RootBeanDefinition;
3233
import org.springframework.core.io.DescriptiveResource;
34+
import org.springframework.tests.sample.beans.ITestBean;
3335
import org.springframework.tests.sample.beans.TestBean;
3436

3537
import static org.junit.Assert.*;
@@ -139,6 +141,21 @@ public void testPostProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScoped
139141
beanFactory.getBean("bar", TestBean.class);
140142
}
141143

144+
@Test
145+
public void testScopedProxyTargetMarkedAsNonAutowireCandidate() {
146+
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
147+
bpp.setBeanFactory(beanFactory);
148+
beanFactory.addBeanPostProcessor(bpp);
149+
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
150+
beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
151+
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
152+
pp.postProcessBeanFactory(beanFactory);
153+
ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
154+
assertTrue(injected instanceof ScopedObject);
155+
assertSame(beanFactory.getBean("scopedClass"), injected);
156+
assertSame(beanFactory.getBean(ITestBean.class), injected);
157+
}
158+
142159
@Test
143160
public void testProcessingAllowedOnlyOncePerProcessorRegistryPair() {
144161
DefaultListableBeanFactory bf1 = new DefaultListableBeanFactory();
@@ -330,6 +347,23 @@ static class LoadedConfig {
330347
}
331348

332349

350+
public static class ScopedProxyConsumer {
351+
352+
@Autowired
353+
public ITestBean testBean;
354+
}
355+
356+
357+
@Configuration
358+
public static class ScopedProxyConfigurationClass {
359+
360+
@Bean @Lazy @Scope(proxyMode=ScopedProxyMode.INTERFACES)
361+
public ITestBean scopedClass() {
362+
return new TestBean();
363+
}
364+
}
365+
366+
333367
public static class Repository<T> {
334368
}
335369

0 commit comments

Comments
 (0)