Introduced support for @Lazy on injection points

This turned into a rather huge affair since it led to the introduction of a new AutowireCandidateResolver implementation in the spring-context module. That ACR impl is now being set through AnnotationConfigUtils; GenericApplicationContext and co do not set a default QualifierAnnotationAutowireCandidateResolver anymore (which has always been a smell anyway).  At the same time, dependency ordering has moved from AutowiredAnnotationBeanPostProcessor to DefaultListableBeanFactory itself through a "dependencyComparator" strategy, applying to constructor dependencies and lazy resolution proxies as well.

Issue: SPR-10353
This commit is contained in:
Juergen Hoeller
2013-08-28 00:14:39 +02:00
parent 01b8d9327d
commit 4447248a83
16 changed files with 554 additions and 49 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;
import org.springframework.tests.sample.beans.ITestBean;
import org.springframework.tests.sample.beans.IndexedTestBean;
@@ -46,16 +47,13 @@ import org.springframework.util.SerializationTestUtils;
import static org.junit.Assert.*;
/**
* Unit tests for {@link AutowiredAnnotationBeanPostProcessor}.
*
* @author Juergen Hoeller
* @author Mark Fisher
* @author Sam Brannen
* @author Chris Beams
*/
public final class AutowiredAnnotationBeanPostProcessorTests {
public class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testIncompleteBeanDefinition() {
@@ -348,6 +346,7 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testOrderedResourceInjection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
@@ -381,6 +380,7 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testAnnotationOrderedResourceInjection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
@@ -412,6 +412,7 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testOrderedCollectionResourceInjection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
@@ -452,6 +453,7 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testAnnotationOrderedCollectionResourceInjection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
@@ -566,6 +568,55 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
bf.destroySingletons();
}
@Test
public void testConstructorResourceInjectionWithMultipleOrderedCandidates() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class));
TestBean tb = new TestBean();
bf.registerSingleton("testBean", tb);
FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
bf.registerSingleton("nestedTestBean1", ntb1);
FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
bf.registerSingleton("nestedTestBean2", ntb2);
ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean");
assertNull(bean.getTestBean3());
assertSame(tb, bean.getTestBean4());
assertEquals(2, bean.getNestedTestBeans().length);
assertSame(ntb2, bean.getNestedTestBeans()[0]);
assertSame(ntb1, bean.getNestedTestBeans()[1]);
bf.destroySingletons();
}
@Test
public void testConstructorResourceInjectionWithMultipleCandidatesAsOrderedCollection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(
ConstructorsCollectionResourceInjectionBean.class));
TestBean tb = new TestBean();
bf.registerSingleton("testBean", tb);
FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
bf.registerSingleton("nestedTestBean1", ntb1);
FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
bf.registerSingleton("nestedTestBean2", ntb2);
ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean");
assertNull(bean.getTestBean3());
assertSame(tb, bean.getTestBean4());
assertEquals(2, bean.getNestedTestBeans().size());
assertSame(ntb2, bean.getNestedTestBeans().get(0));
assertSame(ntb1, bean.getNestedTestBeans().get(1));
bf.destroySingletons();
}
@Test
public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -1102,7 +1153,6 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
private TestBean testBean2;
@Autowired
public void setTestBean2(TestBean testBean2) {
if (this.testBean2 != null) {

View File

@@ -16,10 +16,8 @@
package org.springframework.beans.factory.annotation;
import static org.junit.Assert.assertEquals;
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.AutowireCandidateResolver;
@@ -28,6 +26,9 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import static org.junit.Assert.*;
import static org.springframework.tests.TestResourceUtils.*;
/**
* Unit tests for {@link CustomAutowireConfigurer}.
*
@@ -87,6 +88,11 @@ public final class CustomAutowireConfigurerTests {
public Object getSuggestedValue(DependencyDescriptor descriptor) {
return null;
}
@Override
public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, String beanName) {
return null;
}
}
}