org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinition ( )源码实例Demo

下面列出了org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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"));
	}
}
 
@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"));
	}
}
 
@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"));
	}
}
 
@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));
}
 
@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());
}
 
@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"));
	}
}
 
@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"));
	}
}
 
@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"));
	}
}
 
@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());
}
 
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");
}
 
源代码11 项目: spring-analysis-note   文件: XmlBeanFactoryTests.java
@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);
}
 
源代码12 项目: spring-analysis-note   文件: XmlBeanFactoryTests.java
@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);
}
 
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");
}
 
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");
}
 
@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);
}
 
源代码16 项目: n2o-framework   文件: BeanDefinitionsHolder.java
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);
    }

}
 
@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);
}
 
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));
}
 
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));
}
 
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));
}