BeanDefinition类图继承关系
1.RootBeanDefinition
@Test
public void test2()
{
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
RootBeanDefinition item=new RootBeanDefinition();
item.setBeanClass(TestBean.class);
item.getPropertyValues().add("name","test");
applicationContext.registerBeanDefinition("item",item);
applicationContext.refresh();
TestBean bean= (TestBean) applicationContext.getBean("item");
}
输出结果:
2. ChildBeanDefinition
可以继承RootBeanDefinition的数据
@Test
public void test21()
{
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
{
RootBeanDefinition item=new RootBeanDefinition();
item.setBeanClass(TestBean.class);
item.getPropertyValues().add("name","test");
applicationContext.registerBeanDefinition("item",item);
}
{
ChildBeanDefinition item2=new ChildBeanDefinition("item");
item2.setBeanClass(SubTestBean.class);
item2.getPropertyValues().add("text","text");
applicationContext.registerBeanDefinition("item2",item2);
}
applicationContext.refresh();
TestBean bean= (TestBean) applicationContext.getBean("item");
SubTestBean bean2= (SubTestBean) applicationContext.getBean("item2");
}
输出结果:
3.GenericBeanDefinition
RootBeanDefinition和ChildBeanDefinition可以用GenericBeanDefinition来代替,效果相同
@Test
public void test211()
{
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
{
GenericBeanDefinition item=new GenericBeanDefinition();
item.setBeanClass(TestBean.class);
item.getPropertyValues().add("name","test");
applicationContext.registerBeanDefinition("item",item);
}
{
GenericBeanDefinition item2=new GenericBeanDefinition();
item2.setParentName("item");
item2.setBeanClass(SubTestBean.class);
item2.getPropertyValues().add("text","text");
applicationContext.registerBeanDefinition("item2",item2);
}
applicationContext.refresh();
TestBean bean= (TestBean) applicationContext.getBean("item");
SubTestBean bean2= (SubTestBean) applicationContext.getBean("item2");
}
使用xml加载的bean也是由GenericBeanDefinition来创建
@Test
public void test3()
{
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(BeanDefinitionTest.class,"beanDefinition.xml"));
TestBean bean= (TestBean) applicationContext.getBean("item");
BeanDefinition beanDefinition=applicationContext.getBeanFactory().getBeanDefinition("item");
}
4.AnnotatedGenericBeanDefinition
以@Configuration注解标记的会解析为AnnotatedGenericBeanDefinition
5.ConfigurationClassBeanDefinition
以@Bean注解标记的会解析为ConfigurationClassBeanDefinition
6.ScannedGenericBeanDefinition
以@Component注解标记的会解析为ScannedGenericBeanDefinition
7.BeanDefinitionBuilder
可以使用BeanDefinitionBuilder来构建BeanDefinition
public class BeanDefinitionBuilderTests {
@Test
public void beanClassWithSimpleProperty() {
String[] dependsOn = new String[] { "A", "B", "C" };
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bdb.addPropertyReference("age", "15");
for (int i = 0; i < dependsOn.length; i++) {
bdb.addDependsOn(dependsOn[i]);
}
RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
}
}