spring筆記-BeanDefinition

weixin_34353714發表於2018-03-13

BeanDefinition類圖繼承關係


1297060-b6e420e35f4c5f92.png

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

輸出結果:


1297060-8de4e6e5b4cf5097.png

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

輸出結果:


1297060-2b5dedce42bfe16d.png

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");
    }
1297060-855cf455cd5852d7.png

4.AnnotatedGenericBeanDefinition

以@Configuration註解標記的會解析為AnnotatedGenericBeanDefinition

1297060-fd9d29a17d68108e.png

5.ConfigurationClassBeanDefinition

以@Bean註解標記的會解析為ConfigurationClassBeanDefinition

1297060-7179f1e9af97c9a4.png

6.ScannedGenericBeanDefinition

以@Component註解標記的會解析為ScannedGenericBeanDefinition

1297060-bdf9fc6b8cd78c24.png

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

參考:
http://blog.51cto.com/dba10g/1728512

相關文章