Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setAutowireMode()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setAutowireMode() . 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 vote down vote up
@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());
}
 
Example 2
Source File: AutowireWithExclusionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentFactory() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	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");
	child.registerBeanDefinition("props3", propsDef);
	TestBean rob = (TestBean) child.getBean("rob2");
	assertEquals("props1", rob.getSomeProperties().getProperty("name"));
	assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
 
Example 3
Source File: AutowireWithExclusionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example 4
Source File: AutowireWithExclusionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentFactory() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	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");
	child.registerBeanDefinition("props3", propsDef);
	TestBean rob = (TestBean) child.getBean("rob2");
	assertEquals("props1", rob.getSomeProperties().getProperty("name"));
	assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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]);
}
 
Example 6
Source File: AutowireWithExclusionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	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());
}
 
Example 7
Source File: AutowireWithExclusionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	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());
}
 
Example 8
Source File: AutowireWithExclusionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	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());
}
 
Example 9
Source File: AutowireWithExclusionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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]);
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 13
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 14
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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) {
	}
}
 
Example 15
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 16
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 17
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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) {
	}
}
 
Example 18
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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]);
}
 
Example 19
Source File: AutowireWithExclusionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 20
Source File: AutowireWithExclusionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}