Java Code Examples for org.springframework.tests.sample.beans.factory.DummyFactory#setSingleton()

The following examples show how to use org.springframework.tests.sample.beans.factory.DummyFactory#setSingleton() . 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: BeanFactoryUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithStaticFactory() {
	StaticListableBeanFactory lbf = new StaticListableBeanFactory();
	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	lbf.addBean("t1", t1);
	lbf.addBean("t2", t2);
	lbf.addBean("t3", t3);
	lbf.addBean("t4", t4);

	Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}
 
Example 2
Source File: BeanFactoryUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithStaticFactory() {
	StaticListableBeanFactory lbf = new StaticListableBeanFactory();
	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	lbf.addBean("t1", t1);
	lbf.addBean("t2", t2);
	lbf.addBean("t3", t3);
	lbf.addBean("t4", t4);

	Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}
 
Example 3
Source File: BeanFactoryUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithStaticFactory() {
	StaticListableBeanFactory lbf = new StaticListableBeanFactory();
	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	lbf.addBean("t1", t1);
	lbf.addBean("t2", t2);
	lbf.addBean("t3", t3);
	lbf.addBean("t4", t4);

	Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}
 
Example 4
Source File: BeanFactoryUtilsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithDefaultFactory() {
	Object test3 = this.listableBeanFactory.getBean("test3");
	Object test = this.listableBeanFactory.getBean("test");

	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	this.listableBeanFactory.registerSingleton("t1", t1);
	this.listableBeanFactory.registerSingleton("t2", t2);
	this.listableBeanFactory.registerSingleton("t3", t3);
	this.listableBeanFactory.registerSingleton("t4", t4);

	Map<String, ?> beans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
	assertEquals(6, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);
	// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
	// singleton instances, while testFactory1 and testFactory are *not* found
	// because they are FactoryBean definitions that haven't been initialized yet.

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
	Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
	assertEquals(5, beans.size());
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
	assertEquals(8, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertTrue(beans.get("testFactory2") instanceof TestBean);
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}
 
Example 5
Source File: BeanFactoryUtilsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithDefaultFactory() {
	Object test3 = this.listableBeanFactory.getBean("test3");
	Object test = this.listableBeanFactory.getBean("test");

	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	this.listableBeanFactory.registerSingleton("t1", t1);
	this.listableBeanFactory.registerSingleton("t2", t2);
	this.listableBeanFactory.registerSingleton("t3", t3);
	this.listableBeanFactory.registerSingleton("t4", t4);

	Map<String, ?> beans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
	assertEquals(6, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);
	// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
	// singleton instances, while testFactory1 and testFactory are *not* found
	// because they are FactoryBean definitions that haven't been initialized yet.

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
	Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
	assertEquals(5, beans.size());
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
	assertEquals(8, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertTrue(beans.get("testFactory2") instanceof TestBean);
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}
 
Example 6
Source File: BeanFactoryUtilsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testFindsBeansOfTypeWithDefaultFactory() {
	Object test3 = this.listableBeanFactory.getBean("test3");
	Object test = this.listableBeanFactory.getBean("test");

	TestBean t1 = new TestBean();
	TestBean t2 = new TestBean();
	DummyFactory t3 = new DummyFactory();
	DummyFactory t4 = new DummyFactory();
	t4.setSingleton(false);
	this.listableBeanFactory.registerSingleton("t1", t1);
	this.listableBeanFactory.registerSingleton("t2", t2);
	this.listableBeanFactory.registerSingleton("t3", t3);
	this.listableBeanFactory.registerSingleton("t4", t4);

	Map<String, ?> beans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
	assertEquals(6, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);
	// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
	// singleton instances, while testFactory1 and testFactory are *not* found
	// because they are FactoryBean definitions that haven't been initialized yet.

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
	Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
	assertEquals(5, beans.size());
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
	assertEquals(8, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertTrue(beans.get("testFactory2") instanceof TestBean);
	assertEquals(t1, beans.get("t1"));
	assertEquals(t2, beans.get("t2"));
	assertEquals(t3.getObject(), beans.get("t3"));
	assertTrue(beans.get("t4") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
	assertEquals(t3, beans.get("&t3"));
	assertEquals(t4, beans.get("&t4"));
}