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

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinition() . 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: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatching() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBean");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() {
	String theChildScope = "bonanza!";

	RootBeanDefinition parent = new RootBeanDefinition();
	parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);

	AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
	child.setBeanClass(TestBean.class);
	child.setScope(theChildScope);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parent);
	factory.registerBeanDefinition("child", child);

	AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
	assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
 
Example 3
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatching() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBean");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 4
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBeanFactoryMethod");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 5
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatching() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBean");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() throws Exception {
	String theChildScope = "bonanza!";

	RootBeanDefinition parent = new RootBeanDefinition();
	parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);

	AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
	child.setBeanClass(TestBean.class);
	child.setScope(theChildScope);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parent);
	factory.registerBeanDefinition("child", child);

	AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
	assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
 
Example 7
Source File: NestedBeansElementAttributeRecursionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultLazyInit() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
			new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass()));

	BeanDefinition foo = bf.getBeanDefinition("foo");
	BeanDefinition bar = bf.getBeanDefinition("bar");
	BeanDefinition baz = bf.getBeanDefinition("baz");
	BeanDefinition biz = bf.getBeanDefinition("biz");
	BeanDefinition buz = bf.getBeanDefinition("buz");

	assertThat(foo.isLazyInit(), is(false));
	assertThat(bar.isLazyInit(), is(true));
	assertThat(baz.isLazyInit(), is(false));
	assertThat(biz.isLazyInit(), is(true));
	assertThat(buz.isLazyInit(), is(true));
}
 
Example 8
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBeanFactoryMethod");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 9
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
	bd.setLenientConstructorResolution(false);
	try {
		xbf.getBean("lenientDependencyTestBeanFactoryMethod");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		ex.printStackTrace();
		assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
	}
}
 
Example 10
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testStringConstructorArrayNoTypeNonLenient() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
	bd.setLenientConstructorResolution(false);
	ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
	assertTrue(bean.array instanceof String[]);
	assertEquals(0, ((String[]) bean.array).length);
}
 
Example 11
Source File: BeanAnnotationAttributePropagationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private AbstractBeanDefinition beanDef(Class<?> configClass) {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(factory);
	return (AbstractBeanDefinition) factory.getBeanDefinition("foo");
}
 
Example 12
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testJavaLangStringConstructor() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("string");
	bd.setLenientConstructorResolution(false);
	String str = (String) xbf.getBean("string");
	assertEquals("test", str);
}
 
Example 13
Source File: BeanAnnotationAttributePropagationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private AbstractBeanDefinition beanDef(Class<?> configClass) {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(factory);
	return (AbstractBeanDefinition) factory.getBeanDefinition("foo");
}
 
Example 14
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testStringConstructorArrayNoTypeNonLenient() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
	bd.setLenientConstructorResolution(false);
	ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
	assertTrue(bean.array instanceof String[]);
	assertEquals(0, ((String[]) bean.array).length);
}
 
Example 15
Source File: BeanAnnotationAttributePropagationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private AbstractBeanDefinition beanDef(Class<?> configClass) {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(factory);
	return (AbstractBeanDefinition) factory.getBeanDefinition("foo");
}
 
Example 16
Source File: BeanDefinitionsHolder.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
public void init() {
    DefaultListableBeanFactory beanFactory =
            (DefaultListableBeanFactory) ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    for (String s : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(s);
        String beanClassName = beanDefinition.getBeanClassName();
        if (beanClassName == null)
            continue;
        Class aClass = getClass(beanClassName);
        if (aClass == null)
            continue;
        classes.add(aClass);
    }

}
 
Example 17
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringConstructorArrayNoTypeNonLenient() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
	AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("constructorArrayNoType");
	bd.setLenientConstructorResolution(false);
	ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArrayNoType");
	assertTrue(bean.array instanceof String[]);
	assertEquals(0, ((String[]) bean.array).length);
}
 
Example 18
Source File: JdbcNamespaceIntegrationTests.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
	BeanDefinition bean = factory.getBeanDefinition(expected);
	PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
	assertThat(value, is(notNullValue()));
	assertThat(value.getValue().toString(), is(expected));
}
 
Example 19
Source File: JdbcNamespaceIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
	BeanDefinition bean = factory.getBeanDefinition(expected);
	PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
	assertThat(value, is(notNullValue()));
	assertThat(value.getValue().toString(), is(expected));
}
 
Example 20
Source File: JdbcNamespaceIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
	BeanDefinition bean = factory.getBeanDefinition(expected);
	PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
	assertThat(value, is(notNullValue()));
	assertThat(value.getValue().toString(), is(expected));
}