List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory autowire
@Override public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireWithNoDependencies() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); Object registered = lbf.autowire(NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); assertEquals(1, lbf.getBeanDefinitionCount()); assertTrue(registered instanceof NoDependencies); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireWithSatisfiedJavaBeanDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs);//w ww. j ava2 s . c om lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); // Depends on age, name and spouse (TestBean) Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); assertEquals(1, lbf.getBeanDefinitionCount()); DependenciesBean kerry = (DependenciesBean) registered; TestBean rod = (TestBean) lbf.getBean("rod"); assertSame(rod, kerry.getSpouse()); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireWithSatisfiedConstructorDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs);//from w ww . ja va 2s .co m lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); assertEquals(1, lbf.getBeanDefinitionCount()); ConstructorDependency kerry = (ConstructorDependency) registered; TestBean rod = (TestBean) lbf.getBean("rod"); assertSame(rod, kerry.spouse); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireWithTwoMatchesForConstructorDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod", bd); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("rod2", bd2); try {//from w ww . j a va2s.c om lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected assertTrue(ex.getMessage().contains("rod")); assertTrue(ex.getMessage().contains("rod2")); } }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireWithUnsatisfiedConstructorDependency() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.setPropertyValues(pvs);//w w w. j ava2 s .c o m lbf.registerBeanDefinition("rod", bd); assertEquals(1, lbf.getBeanDefinitionCount()); try { lbf.autowire(UnsatisfiedConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); fail("Should have unsatisfied constructor dependency on SideEffectBean"); } catch (UnsatisfiedDependencyException ex) { // expected } }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireConstructor() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); ConstructorDependenciesBean bean = (ConstructorDependenciesBean) lbf .autowire(ConstructorDependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true); Object spouse = lbf.getBean("spouse"); assertTrue(bean.getSpouse1() == spouse); assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireBeanByName() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); TestBean spouse = (TestBean) lbf.getBean("spouse"); assertEquals(spouse, bean.getSpouse()); assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireBeanByNameWithDependencyCheck() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); try {/* w w w . ja v a 2s . c o m*/ lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException ex) { // expected } }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireBeanByNameWithNoDependencyCheck() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spous", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); assertNull(bean.getSpouse());//from ww w. ja va 2 s. c om }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testAutowireBeanByType() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("test", bd); DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); TestBean test = (TestBean) lbf.getBean("test"); assertEquals(test, bean.getSpouse()); }