spring筆記-BeanDefinition
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();
}
}
相關文章
- spring學習:spring原始碼_BeanDefinitionSpring原始碼Bean
- 【spring原始碼系列】之【BeanDefinition】Spring原始碼Bean
- Spring中眼花繚亂的BeanDefinitionSpringBean
- 深入Spring之IOC之載入BeanDefinitionSpringBean
- Spring原始碼系列:BeanDefinition載入(下)Spring原始碼Bean
- Spring原始碼系列:BeanDefinition載入(中)Spring原始碼Bean
- Spring原始碼系列:BeanDefinition載入(上)Spring原始碼Bean
- Spring原始碼系列:BeanDefinition原始碼解析Spring原始碼Bean
- 筆記:Spring筆記Spring
- Spring原始碼之IOC(一)BeanDefinition原始碼解析Spring原始碼Bean
- spring筆記三Spring筆記
- Spring筆記二Spring筆記
- spring boot 筆記Spring Boot筆記
- Spring:BeanDefinition&PostProcessor不瞭解一下嗎?SpringBean
- BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)BeanSpring套件namespace
- Spring MVC for beginners 筆記SpringMVC筆記
- spring boot AOP筆記Spring Boot筆記
- spring IOC/DI筆記Spring筆記
- Spring基礎筆記Spring筆記
- Spring事務筆記Spring筆記
- spring學習筆記Spring筆記
- Spring Security OAuth 筆記SpringOAuth筆記
- Spring 學習筆記Spring筆記
- BeanDefinition元資訊Bean
- spring 掃描BeanDefinition詳解以及TypeFilter擴充套件點SpringBeanFilter套件
- Spring原始碼解讀(1)-IOC容器BeanDefinition的載入Spring原始碼Bean
- spring-IOC容器原始碼分析(二)BeanDefinition註冊流程Spring原始碼Bean
- Spring 學習筆記(2) Spring BeanSpring筆記Bean
- Spring 學習筆記(3)Spring MVCSpring筆記MVC
- Spring筆記(6) - Spring的BeanFactoryPostProcessor探究Spring筆記Bean
- Spring筆記(1) - 工廠Spring筆記
- spring學習筆記(1)Spring筆記
- Spring筆記(四)整合MybatisSpring筆記MyBatis
- Spring學習筆記(一)Spring筆記
- 《Spring In Action》讀書筆記Spring筆記
- Spring原始碼之預設標籤解析及BeanDefinition註冊Spring原始碼Bean
- Spring4原始碼分析--BeanDefinition解析與載入(AOP部分)Spring原始碼Bean
- Spring Security Filter 學習筆記SpringFilter筆記