Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#setDependencyComparator()
The following examples show how to use
org.springframework.beans.factory.support.DefaultListableBeanFactory#setDependencyComparator() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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); lbf.registerBeanDefinition("spouse", bd2); try { lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertNotNull("Exception should have cause", ex.getCause()); assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass()); assertTrue(ex.getMessage().contains("5")); // conflicting priority } }
Example 2
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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(); }
Example 3
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@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); lbf.registerBeanDefinition("spouse", bd2); try { lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertNotNull("Exception should have cause", ex.getCause()); assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass()); assertTrue(ex.getMessage().contains("5")); // conflicting priority } }
Example 4
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testMapInjectionWithPriority() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class); RootBeanDefinition bd3 = new RootBeanDefinition(NullTestBeanFactoryBean.class); RootBeanDefinition bd4 = new RootBeanDefinition(TestBeanRecipient.class, RootBeanDefinition.AUTOWIRE_CONSTRUCTOR, false); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); lbf.registerBeanDefinition("bd3", bd3); lbf.registerBeanDefinition("bd4", bd4); lbf.preInstantiateSingletons(); TestBean bean = lbf.getBean(TestBeanRecipient.class).testBean; assertThat(bean.getBeanName(), equalTo("bd1")); }
Example 5
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("test"))); }
Example 6
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAnnotationOrderedResourceInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); 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); FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean(); bf.registerSingleton("nestedTestBean1", ntb1); FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertSame(tb, bean.getTestBean2()); assertSame(tb, bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertSame(itb, bean.getIndexedTestBean()); assertEquals(2, bean.getNestedTestBeans().length); assertSame(ntb2, bean.getNestedTestBeans()[0]); assertSame(ntb1, bean.getNestedTestBeans()[1]); assertEquals(2, bean.nestedTestBeansField.length); assertSame(ntb2, bean.nestedTestBeansField[0]); assertSame(ntb1, bean.nestedTestBeansField[1]); bf.destroySingletons(); }
Example 7
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@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); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse"))); }
Example 8
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@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); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("test"))); }
Example 9
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPrimary(true); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd2")); }
Example 10
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithPriorityAndNullInstance() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd1")); }
Example 11
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); lbf.registerBeanDefinition("bd2", bd2); thrown.expect(NoUniqueBeanDefinitionException.class); thrown.expectMessage(containsString("Multiple beans found with the same priority")); thrown.expectMessage(containsString("5")); // conflicting priority lbf.getBean(TestBean.class); }
Example 12
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd2")); }
Example 13
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse"))); }
Example 14
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@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); lbf.registerBeanDefinition("test", bd); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse"))); }
Example 15
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@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); lbf.registerBeanDefinition("spouse", bd2); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertThat(bean.getSpouse(), equalTo(lbf.getBean("test"))); }
Example 16
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testAnnotationOrderedCollectionResourceInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); IndexedTestBean itb = new IndexedTestBean(); bf.registerSingleton("indexedTestBean", itb); FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean(); bf.registerSingleton("nestedTestBean1", ntb1); FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertSame(tb, bean.getTestBean2()); assertSame(tb, bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertSame(itb, bean.getIndexedTestBean()); assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); assertEquals(2, bean.nestedTestBeansSetter.size()); assertSame(ntb2, bean.nestedTestBeansSetter.get(0)); assertSame(ntb1, bean.nestedTestBeansSetter.get(1)); assertEquals(2, bean.nestedTestBeansField.size()); assertSame(ntb2, bean.nestedTestBeansField.get(0)); assertSame(ntb1, bean.nestedTestBeansField.get(1)); bf.destroySingletons(); }
Example 17
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithPriorityAndNullInstance() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd1")); }
Example 18
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithMultiplePriority() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> lbf.getBean(TestBean.class)) .withMessageContaining("Multiple beans found with the same priority") .withMessageContaining("5"); // conflicting priority }
Example 19
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testOrderedCollectionResourceInjection() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); IndexedTestBean itb = new IndexedTestBean(); bf.registerSingleton("indexedTestBean", itb); OrderedNestedTestBean ntb1 = new OrderedNestedTestBean(); ntb1.setOrder(2); bf.registerSingleton("nestedTestBean1", ntb1); OrderedNestedTestBean ntb2 = new OrderedNestedTestBean(); ntb2.setOrder(1); bf.registerSingleton("nestedTestBean2", ntb2); // Two calls to verify that caching doesn't break re-creation. OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertSame(tb, bean.getTestBean2()); assertSame(tb, bean.getTestBean3()); assertSame(tb, bean.getTestBean4()); assertSame(itb, bean.getIndexedTestBean()); assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); assertEquals(2, bean.nestedTestBeansSetter.size()); assertSame(ntb2, bean.nestedTestBeansSetter.get(0)); assertSame(ntb1, bean.nestedTestBeansSetter.get(1)); assertEquals(2, bean.nestedTestBeansField.size()); assertSame(ntb2, bean.nestedTestBeansField.get(0)); assertSame(ntb1, bean.nestedTestBeansField.get(1)); bf.destroySingletons(); }
Example 20
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithPriority() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class); RootBeanDefinition bd2 = new RootBeanDefinition(LowPriorityTestBean.class); RootBeanDefinition bd3 = new RootBeanDefinition(NullTestBeanFactoryBean.class); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); lbf.registerBeanDefinition("bd3", bd3); lbf.preInstantiateSingletons(); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd1")); }