Refactored support for @Order on @Bean methods as well as @Priority handling
Issue: SPR-11310 Issue: SPR-10548
This commit is contained in:
@@ -69,6 +69,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.ConstructorDependenciesBean;
|
||||
import org.springframework.beans.propertyeditors.CustomNumberEditor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
@@ -1392,6 +1393,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testGetBeanByTypeWithPriority() throws Exception {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class);
|
||||
lbf.registerBeanDefinition("bd1", bd1);
|
||||
@@ -1403,6 +1405,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testGetBeanByTypeWithMultiplePriority() throws Exception {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
lbf.registerBeanDefinition("bd1", bd1);
|
||||
@@ -1416,6 +1419,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testGetBeanByTypeWithPriorityAndNullInstance() throws Exception {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class);
|
||||
lbf.registerBeanDefinition("bd1", bd1);
|
||||
@@ -1427,6 +1431,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() throws Exception {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
|
||||
bd2.setPrimary(true);
|
||||
@@ -1697,6 +1702,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testAutowireBeanByTypeWithTwoMatchesAndPriority() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class);
|
||||
lbf.registerBeanDefinition("test", bd);
|
||||
@@ -1710,6 +1716,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testAutowireBeanByTypeWithIdenticalPriorityCandidates() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
lbf.registerBeanDefinition("test", bd);
|
||||
@@ -1730,6 +1737,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
@Test
|
||||
public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
|
||||
bd2.setPrimary(true);
|
||||
|
||||
@@ -39,11 +39,11 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
|
||||
import org.springframework.beans.factory.support.DefaultDependencyComparator;
|
||||
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;
|
||||
@@ -354,7 +354,7 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
@Test
|
||||
public void testOrderedResourceInjection() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
@@ -385,38 +385,10 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
bf.destroySingletons();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrderedResourceInjectionDetectsFactoryAwareComparator() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
DefaultDependencyComparator comparator = mock(DefaultDependencyComparator.class);
|
||||
bf.setDependencyComparator(comparator);
|
||||
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
|
||||
TestBean tb = new TestBean();
|
||||
bf.registerSingleton("testBean", tb);
|
||||
IndexedTestBean itb = new IndexedTestBean();
|
||||
bf.registerSingleton("indexedTestBean", itb);
|
||||
final OrderedNestedTestBean ntb1 = new OrderedNestedTestBean();
|
||||
ntb1.setOrder(2);
|
||||
bf.registerSingleton("nestedTestBean1", ntb1);
|
||||
final OrderedNestedTestBean ntb2 = new OrderedNestedTestBean();
|
||||
ntb2.setOrder(1);
|
||||
bf.registerSingleton("nestedTestBean2", ntb2);
|
||||
|
||||
OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
|
||||
verify(comparator, never()).compare(any(), any());
|
||||
verify(comparator, never()).sortList(any(),any());
|
||||
verify(comparator, times(2)).sortArray(any(),any());
|
||||
bf.destroySingletons();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationOrderedResourceInjection() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
@@ -448,7 +420,7 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
@Test
|
||||
public void testOrderedCollectionResourceInjection() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
@@ -489,7 +461,7 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
@Test
|
||||
public void testAnnotationOrderedCollectionResourceInjection() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
@@ -607,7 +579,7 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
@Test
|
||||
public void testConstructorResourceInjectionWithMultipleOrderedCandidates() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
@@ -631,7 +603,7 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||
@Test
|
||||
public void testConstructorResourceInjectionWithMultipleCandidatesAsOrderedCollection() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.setDependencyComparator(DefaultDependencyComparator.INSTANCE);
|
||||
bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
|
||||
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||
bpp.setBeanFactory(bf);
|
||||
bf.addBeanPostProcessor(bpp);
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DependencyComparatorTests {
|
||||
|
||||
private final DefaultDependencyComparator comparator = new DefaultDependencyComparator();
|
||||
|
||||
@Test
|
||||
public void plainComparator() {
|
||||
List<Object> items = new ArrayList<Object>();
|
||||
C c = new C(5);
|
||||
C c2 = new C(-5);
|
||||
items.add(c);
|
||||
items.add(c2);
|
||||
Collections.sort(items, comparator);
|
||||
assertOrder(items, c2, c);
|
||||
}
|
||||
|
||||
private void assertOrder(List<?> actual, Object... expected) {
|
||||
for (int i = 0; i < actual.size(); i++) {
|
||||
assertSame("Wrong instance at index '" + i + "'", expected[i], actual.get(i));
|
||||
}
|
||||
assertEquals("Wrong number of items", expected.length, actual.size());
|
||||
}
|
||||
|
||||
private static class C implements Ordered {
|
||||
private final int order;
|
||||
|
||||
private C(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestName;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class FactoryAwareOrderProviderTests {
|
||||
|
||||
@Rule
|
||||
public final TestName name = new TestName();
|
||||
|
||||
@Mock
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noBeanName() {
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(new HashMap<Object, String>());
|
||||
assertNull(orderProvider.getOrder(25));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanNameNotRegistered() {
|
||||
HashMap<Object, String> beans = new HashMap<>();
|
||||
beans.put(25, "myBean");
|
||||
given(beanFactory.containsBeanDefinition("myBean")).willReturn(false);
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(beans);
|
||||
assertNull(orderProvider.getOrder(25));
|
||||
verify(beanFactory).containsBeanDefinition("myBean");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void beanNameNoRootBeanDefinition() {
|
||||
HashMap<Object, String> beans = new HashMap<>();
|
||||
beans.put(25, "myBean");
|
||||
given(beanFactory.containsBeanDefinition("myBean")).willReturn(true);
|
||||
given(beanFactory.getMergedBeanDefinition("myBean")).willReturn(mock(BeanDefinition.class));
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(beans);
|
||||
assertNull(orderProvider.getOrder(25));
|
||||
verify(beanFactory).containsBeanDefinition("myBean");
|
||||
verify(beanFactory).getMergedBeanDefinition("myBean");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanNameNoFactory() {
|
||||
HashMap<Object, String> beans = new HashMap<>();
|
||||
beans.put(25, "myBean");
|
||||
RootBeanDefinition rbd = mock(RootBeanDefinition.class);
|
||||
given(rbd.getResolvedFactoryMethod()).willReturn(null);
|
||||
|
||||
given(beanFactory.containsBeanDefinition("myBean")).willReturn(true);
|
||||
given(beanFactory.getMergedBeanDefinition("myBean")).willReturn(rbd);
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(beans);
|
||||
assertNull(orderProvider.getOrder(25));
|
||||
verify(beanFactory).containsBeanDefinition("myBean");
|
||||
verify(beanFactory).getMergedBeanDefinition("myBean");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanNameFactoryNoOrderValue() {
|
||||
HashMap<Object, String> beans = new HashMap<>();
|
||||
beans.put(25, "myBean");
|
||||
|
||||
Method m = ReflectionUtils.findMethod(getClass(), name.getMethodName());
|
||||
RootBeanDefinition rbd = mock(RootBeanDefinition.class);
|
||||
given(rbd.getResolvedFactoryMethod()).willReturn(m);
|
||||
|
||||
given(beanFactory.containsBeanDefinition("myBean")).willReturn(true);
|
||||
given(beanFactory.getMergedBeanDefinition("myBean")).willReturn(rbd);
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(beans);
|
||||
assertNull(orderProvider.getOrder(25));
|
||||
verify(beanFactory).containsBeanDefinition("myBean");
|
||||
verify(beanFactory).getMergedBeanDefinition("myBean");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(500)
|
||||
public void beanNameFactoryOrderValue() {
|
||||
HashMap<Object, String> beans = new HashMap<>();
|
||||
beans.put(25, "myBean");
|
||||
|
||||
Method m = ReflectionUtils.findMethod(getClass(), name.getMethodName());
|
||||
RootBeanDefinition rbd = mock(RootBeanDefinition.class);
|
||||
given(rbd.getResolvedFactoryMethod()).willReturn(m);
|
||||
|
||||
given(beanFactory.containsBeanDefinition("myBean")).willReturn(true);
|
||||
given(beanFactory.getMergedBeanDefinition("myBean")).willReturn(rbd);
|
||||
FactoryAwareOrderProvider orderProvider = createOrderProvider(beans);
|
||||
assertEquals(Integer.valueOf(500), orderProvider.getOrder(25));
|
||||
verify(beanFactory).containsBeanDefinition("myBean");
|
||||
verify(beanFactory).getMergedBeanDefinition("myBean");
|
||||
}
|
||||
|
||||
private FactoryAwareOrderProvider createOrderProvider(HashMap<Object, String> beans) {
|
||||
return new FactoryAwareOrderProvider(beans, beanFactory);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user