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

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanNamesForType() . 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 spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
	lbf.getBean("&factoryBean");

	String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
	lbf.getBean("&factoryBean");

	String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setFactoryMethodName("createTestBean");
	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"));
	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));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
	assertFalse(lbf.containsSingleton("factoryBean"));

	String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setFactoryMethodName("createTestBean");
	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"));
	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));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
	lbf.getBean("&factoryBean");

	String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);

	beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
	assertEquals(1, beanNames.length);
	assertEquals("&factoryBean", beanNames[0]);
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "true");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	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.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
	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);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
	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);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 13
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "true");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example 14
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	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.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 15
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(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", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
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: UnitTestDependencyInjectionTestExecutionListener.java    From wisp with Apache License 2.0 4 votes vote down vote up
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    super.afterTestMethod(testContext);

    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) testContext.getApplicationContext()
            .getAutowireCapableBeanFactory();

    /**
     * 方法结束后记录被测试对象的bean名称
     */
    Object bean = testContext.getTestInstance();
    List<Field> fields = getDeclaredFields(bean);
    for (Field field : fields) {
        InjectMocks injectMocks = field.getAnnotation(InjectMocks.class);
        if (injectMocks == null) {
            continue;
        }
        Object testedBean = null;
        String testedBeanName = null;
        /**
         * 被测试的对象如果通过spring自动注入,则记录
         * 两种注入方式 Autowired
         * Resource
         */
        if (field.getAnnotation(Autowired.class) != null) {
            Qualifier qualifier = field.getAnnotation(Qualifier.class);
            testedBean = qualifier == null ? beanFactory.getBean(field.getType())
                    : beanFactory.getBean(qualifier.value());
            testedBeanName = qualifier == null ? beanFactory.getBeanNamesForType(field.getType())[0]
                    : qualifier.value();
        } else if (field.getAnnotation(Resource.class) != null) {
            Resource resource = field.getAnnotation(Resource.class);
            Class<?> type = resource.type();
            String name = resource.name();
            if (StringUtils.isNotEmpty(name)) {
                testedBean = beanFactory.getBean(name);
                testedBeanName = name;
            } else {
                testedBean = (type != Object.class) ? beanFactory.getBean(type)
                        : beanFactory.getBean(field.getType());
                testedBeanName = (type != Object.class) ? beanFactory.getBeanNamesForType(type)[0]
                        : beanFactory.getBeanNamesForType(field.getType())[0];
            }
        }

        if (testedBean != null) {
            testedObjects.put(testedBeanName, testedBean);
        }
    }
}
 
Example 18
Source File: ApiKeysCacheService.java    From gravitee-gateway with Apache License 2.0 4 votes vote down vote up
@Override
protected void doStart() throws Exception {
    if (enabled) {
        super.doStart();

        LOGGER.info("Overriding API key repository implementation with cached API Key repository");
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext.getParent()).getBeanFactory();

        this.apiKeyRepository = beanFactory.getBean(ApiKeyRepository.class);
        LOGGER.debug("Current API key repository implementation is {}", apiKeyRepository.getClass().getName());

        String [] beanNames = beanFactory.getBeanNamesForType(ApiKeyRepository.class);
        String oldBeanName = beanNames[0];

        beanFactory.destroySingleton(oldBeanName);

        LOGGER.debug("Register API key repository implementation {}", ApiKeyRepositoryWrapper.class.getName());
        beanFactory.registerSingleton(ApiKeyRepository.class.getName(),
                new ApiKeyRepositoryWrapper(this.apiKeyRepository, cache));

        eventManager.subscribeForEvents(this, ReactorEvent.class);

        executorService = Executors.newScheduledThreadPool(threads, new ThreadFactory() {
                    private int counter = 0;
                    private String prefix = "apikeys-refresher";

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, prefix + '-' + counter++);
                    }
                });

        LOGGER.info("Associate a new HTTP handler on {}", PATH);

        // Create handlers
        // Set API-keys handler
        ApiKeysServiceHandler apiKeysHandler = new ApiKeysServiceHandler((ScheduledThreadPoolExecutor) executorService);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(apiKeysHandler);
        router.get(PATH).produces(MediaType.APPLICATION_JSON).handler(apiKeysHandler);

        // Set API handler
        apiKeyHandler = new ApiKeyHandler();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(apiKeyHandler);
        router.get(PATH + "/:apiId").produces(MediaType.APPLICATION_JSON).handler(apiKeyHandler);
    }
}
 
Example 19
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 20
Source File: SubscriptionsCacheService.java    From gravitee-gateway with Apache License 2.0 4 votes vote down vote up
@Override
protected void doStart() throws Exception {
    if (enabled) {
        super.doStart();

        LOGGER.info("Overriding subscription repository implementation with a cached subscription repository");
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext.getParent()).getBeanFactory();

        this.subscriptionRepository = beanFactory.getBean(SubscriptionRepository.class);
        LOGGER.debug("Current subscription repository implementation is {}", subscriptionRepository.getClass().getName());

        String [] beanNames = beanFactory.getBeanNamesForType(SubscriptionRepository.class);
        String oldBeanName = beanNames[0];

        beanFactory.destroySingleton(oldBeanName);

        LOGGER.debug("Register subscription repository implementation {}", SubscriptionRepositoryWrapper.class.getName());
        beanFactory.registerSingleton(SubscriptionRepository.class.getName(),
                new SubscriptionRepositoryWrapper(this.subscriptionRepository, cache));

        eventManager.subscribeForEvents(this, ReactorEvent.class);

        executorService = Executors.newScheduledThreadPool(threads, new ThreadFactory() {
                    private int counter = 0;
                    private String prefix = "subscriptions-refresher";

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, prefix + '-' + counter++);
                    }
                });

        LOGGER.info("Associate a new HTTP handler on {}", PATH);

        // Create handlers
        // Set subscriptions handler
        SubscriptionsServiceHandler subscriptionsServiceHandler = new SubscriptionsServiceHandler((ScheduledThreadPoolExecutor) executorService);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(subscriptionsServiceHandler);
        router.get(PATH).produces(MediaType.APPLICATION_JSON).handler(subscriptionsServiceHandler);

        // Set API subscriptions handler
        apiSubscriptionsHandler = new ApiSubscriptionsHandler();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(apiSubscriptionsHandler);
        router.get(PATH + "/:apiId").produces(MediaType.APPLICATION_JSON).handler(apiSubscriptionsHandler);
    }
}