/** * Create a new RootBeanDefinition for a singleton, * using the given autowire mode. * @param beanClass the class of the bean to instantiate * @param autowireMode by name or type, using the constants in this interface * @param dependencyCheck whether to perform a dependency check for objects * (not applicable to autowiring a constructor, thus ignored there) */ public RootBeanDefinition(@Nullable Class<?> beanClass, int autowireMode, boolean dependencyCheck) { super(); setBeanClass(beanClass); setAutowireMode(autowireMode); if (dependencyCheck && getResolvedAutowireMode() != AUTOWIRE_CONSTRUCTOR) { setDependencyCheck(DEPENDENCY_CHECK_OBJECTS); } }
@Test public void testAvoidCircularReferenceThroughAutowiring() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); RootBeanDefinition bd2 = new RootBeanDefinition(String.class); bd2.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("string", bd2); lbf.preInstantiateSingletons(); }
@Test public void testGenericSetConstructorWithAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertTrue(gb.getIntegerSet().contains(new Integer(4))); assertTrue(gb.getIntegerSet().contains(new Integer(5))); }
@Test public void testArrayConstructorWithAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); }
@Test public void testCircularReferenceThroughAutowiring() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); try { lbf.preInstantiateSingletons(); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException expected) { } }
@Test public void testCircularReferenceThroughFactoryBeanAutowiring() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); try { lbf.preInstantiateSingletons(); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException expected) { } }
@Test public void testCircularReferenceThroughFactoryBeanTypeCheck() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class); bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); lbf.registerBeanDefinition("test", bd); try { lbf.getBeansOfType(String.class); fail("Should have thrown UnsatisfiedDependencyException"); } catch (UnsatisfiedDependencyException expected) { } }
@Test public void testArrayPropertyWithOptionalAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertNull(ab.getResourceArray()); }
@Test public void testArrayConstructorWithOptionalAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertNull(ab.getIntegerArray()); }
@Test public void testGenericListPropertyWithOptionalAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertNull(gb.getResourceList()); }
@Test public void testGenericSetConstructorWithOptionalAutowiring() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertNull(gb.getIntegerSet()); }
@Test public void testGenericSetListConstructorWithAutowiring() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertTrue(gb.getIntegerSet().contains(new Integer(4))); assertTrue(gb.getIntegerSet().contains(new Integer(5))); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); }
@Test public void testGenericListPropertyWithAutowiring() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(GenericIntegerBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("genericBean", rbd); GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("genericBean"); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); }
@Test public void byTypeAutowireWithExclusionInParentFactory() throws Exception { CountingFactory.reset(); DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml"); parent.preInstantiateSingletons(); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class); robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally")); child.registerBeanDefinition("rob2", robDef); TestBean rob = (TestBean) child.getBean("rob2"); assertEquals("props1", rob.getSomeProperties().getProperty("name")); assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); }
@Test public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }
@Test public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception { CountingFactory.reset(); DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml"); parent.preInstantiateSingletons(); DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent); RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class); robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally")); child.registerBeanDefinition("rob2", robDef); RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class); propsDef.getPropertyValues().add("properties", "name=props3"); propsDef.setPrimary(true); child.registerBeanDefinition("props3", propsDef); TestBean rob = (TestBean) child.getBean("rob2"); assertEquals("props3", rob.getSomeProperties().getProperty("name")); assertEquals(1, CountingFactory.getFactoryBeanInstanceCount()); }
@Test public void testConfigureBeanWithAutowiring() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); lbf.registerBeanDefinition("spouse", bd); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", "99"); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); tbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_NAME); lbf.registerBeanDefinition("test", tbd); TestBean tb = new TestBean(); lbf.configureBean(tb, "test"); assertSame(lbf, tb.getBeanFactory()); TestBean spouse = (TestBean) lbf.getBean("spouse"); assertEquals(spouse, tb.getSpouse()); }
@Test public void testGenericSetListConstructorWithOptionalAutowiring() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertNull(gb.getIntegerSet()); assertNull(gb.getResourceList()); }
@Test public void testDoubleArrayConstructorWithOptionalAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertNull(ab.getIntegerArray()); assertNull(ab.getResourceArray()); }
@Test public void testArrayPropertyWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }