Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#registerAlias()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#registerAlias() . 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 testCanReferenceParentBeanFromChildViaAlias() {
	final String EXPECTED_NAME = "Juergen";
	final int EXPECTED_AGE = 41;

	RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
	parentDefinition.setAbstract(true);
	parentDefinition.getPropertyValues().add("name", EXPECTED_NAME);
	parentDefinition.getPropertyValues().add("age", EXPECTED_AGE);

	ChildBeanDefinition childDefinition = new ChildBeanDefinition("alias");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parentDefinition);
	factory.registerBeanDefinition("child", childDefinition);
	factory.registerAlias("parent", "alias");

	TestBean child = (TestBean) factory.getBean("child");
	assertEquals(EXPECTED_NAME, child.getName());
	assertEquals(EXPECTED_AGE, child.getAge());

	assertEquals("Use cached merged bean definition",
			factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCanReferenceParentBeanFromChildViaAlias() {
	final String EXPECTED_NAME = "Juergen";
	final int EXPECTED_AGE = 41;

	RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
	parentDefinition.setAbstract(true);
	parentDefinition.getPropertyValues().add("name", EXPECTED_NAME);
	parentDefinition.getPropertyValues().add("age", EXPECTED_AGE);

	ChildBeanDefinition childDefinition = new ChildBeanDefinition("alias");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parentDefinition);
	factory.registerBeanDefinition("child", childDefinition);
	factory.registerAlias("parent", "alias");

	TestBean child = (TestBean) factory.getBean("child");
	assertEquals(EXPECTED_NAME, child.getName());
	assertEquals(EXPECTED_AGE, child.getAge());

	assertEquals("Use cached merged bean definition",
			factory.getMergedBeanDefinition("child"), factory.getMergedBeanDefinition("child"));
}
 
Example 3
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testExtendedEjbInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedEjbInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedEjbInjectionBean bean = (ExtendedEjbInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAliasChaining() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "testAlias");
	lbf.registerAlias("testAlias", "testAlias2");
	lbf.registerAlias("testAlias2", "testAlias3");
	Object bean = lbf.getBean("test");
	assertSame(bean, lbf.getBean("testAlias"));
	assertSame(bean, lbf.getBean("testAlias2"));
	assertSame(bean, lbf.getBean("testAlias3"));
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAliasChaining() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "testAlias");
	lbf.registerAlias("testAlias", "testAlias2");
	lbf.registerAlias("testAlias2", "testAlias3");
	Object bean = lbf.getBean("test");
	assertSame(bean, lbf.getBean("testAlias"));
	assertSame(bean, lbf.getBean("testAlias2"));
	assertSame(bean, lbf.getBean("testAlias3"));
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBeanDefinitionRemoval() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setAllowBeanDefinitionOverriding(false);
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerAlias("test", "test2");
	lbf.preInstantiateSingletons();
	lbf.removeBeanDefinition("test");
	lbf.removeAlias("test2");
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "test2");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("test2") instanceof NestedTestBean);
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBeanDefinitionOverriding() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("otherTest", "test2");
	lbf.registerAlias("test", "test2");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("test2") instanceof NestedTestBean);
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeanDefinitionRemoval() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setAllowBeanDefinitionOverriding(false);
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerAlias("test", "test2");
	lbf.preInstantiateSingletons();
	lbf.removeBeanDefinition("test");
	lbf.removeAlias("test2");
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "test2");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("test2") instanceof NestedTestBean);
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAliasChaining() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "testAlias");
	lbf.registerAlias("testAlias", "testAlias2");
	lbf.registerAlias("testAlias2", "testAlias3");
	Object bean = lbf.getBean("test");
	assertSame(bean, lbf.getBean("testAlias"));
	assertSame(bean, lbf.getBean("testAlias2"));
	assertSame(bean, lbf.getBean("testAlias3"));
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBeanDefinitionOverridingWithAlias() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerAlias("test", "testAlias");
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "testAlias");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("testAlias") instanceof NestedTestBean);
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBeanDefinitionRemoval() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setAllowBeanDefinitionOverriding(false);
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerAlias("test", "test2");
	lbf.preInstantiateSingletons();
	lbf.removeBeanDefinition("test");
	lbf.removeAlias("test2");
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "test2");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("test2") instanceof NestedTestBean);
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeanDefinitionOverridingWithAlias() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.registerAlias("test", "testAlias");
	lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
	lbf.registerAlias("test", "testAlias");
	assertTrue(lbf.getBean("test") instanceof NestedTestBean);
	assertTrue(lbf.getBean("testAlias") instanceof NestedTestBean);
}
 
Example 13
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 14
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 15
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertTrue(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertTrue(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertTrue(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertTrue(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertTrue(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertTrue(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertTrue(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(DummyFactory.class, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 16
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 17
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertTrue(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertTrue(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertTrue(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertTrue(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertTrue(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertTrue(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertTrue(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(DummyFactory.class, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 18
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertTrue(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertTrue(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertTrue(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertTrue(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertTrue(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertTrue(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertTrue(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(DummyFactory.class, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
 
Example 19
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtendedResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Properties props = new Properties();
	props.setProperty("tb", "testBean3");
	ppc.setProperties(props);
	ppc.postProcessBeanFactory(bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
	bf.registerBeanDefinition("annotatedBean3", new RootBeanDefinition(ConvertedResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("value", "5");
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	NamedResourceInjectionBean bean2 = (NamedResourceInjectionBean) bf.getBean("annotatedBean2");
	assertSame(tb6, bean2.testBean);

	ConvertedResourceInjectionBean bean3 = (ConvertedResourceInjectionBean) bf.getBean("annotatedBean3");
	assertSame(5, bean3.value);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 20
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}