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原始碼系列:BeanDefinition載入(下)Spring原始碼Bean
- 深入Spring之IOC之載入BeanDefinitionSpringBean
- 筆記:Spring筆記Spring
- spring筆記三Spring筆記
- Spring筆記二Spring筆記
- Spring原始碼之IOC(一)BeanDefinition原始碼解析Spring原始碼Bean
- spring學習筆記Spring筆記
- spring boot AOP筆記Spring Boot筆記
- spring IOC/DI筆記Spring筆記
- Spring基礎筆記Spring筆記
- Spring MVC for beginners 筆記SpringMVC筆記
- Spring事務筆記Spring筆記
- Spring Security OAuth 筆記SpringOAuth筆記
- Spring 學習筆記Spring筆記
- Spring筆記(6) - Spring的BeanFactoryPostProcessor探究Spring筆記Bean
- Spring 學習筆記(2) Spring BeanSpring筆記Bean
- Spring 學習筆記(3)Spring MVCSpring筆記MVC
- BeanDefinition註冊流程、spring 擴充套件點一(NamespaceHandler)BeanSpring套件namespace
- Spring:BeanDefinition&PostProcessor不瞭解一下嗎?SpringBean
- Spring學習筆記(一)Spring筆記
- Spring筆記(1) - 工廠Spring筆記
- Spring筆記(四)整合MybatisSpring筆記MyBatis
- spring學習筆記(1)Spring筆記
- spring 掃描BeanDefinition詳解以及TypeFilter擴充套件點SpringBeanFilter套件
- spring-IOC容器原始碼分析(二)BeanDefinition註冊流程Spring原始碼Bean
- Spring原始碼解讀(1)-IOC容器BeanDefinition的載入Spring原始碼Bean
- Spring Boot學習筆記:Spring Boot核心配置Spring Boot筆記
- spring下 -spring整體架構,JdbcTemplate筆記Spring架構JDBC筆記
- 《Spring實戰》讀書筆記 #每日筆記 100-004Spring筆記
- spring-5學習筆記Spring筆記
- Spring筆記——SpringBoot啟動流程筆記Spring Boot
- Spring MVC學習筆記二SpringMVC筆記
- Spring框架學習筆記(1)Spring框架筆記
- Spring Boot系列筆記--整合RedisSpring Boot筆記Redis
- Spring學習筆記目錄Spring筆記