Spring笔记之五(分层 Bean Factory 使用)

本文研究Spring分层Bean Factory用法,先看有关代码:
 1 parent .xml
 2 
 3  <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
      "http://www.springframework.org/dtd/spring-beans.dtd"
>
 4  < beans >
 5       < bean  id ="injectBean"  class ="java.lang.String" >
 6              < constructor-arg >
 7                  < value > Bean In Parent </ value >
 8              </ constructor-arg >
 9       </ bean >
10       < bean  id ="injectBeanParent"  class ="java.lang.String" >
11              < constructor-arg >
12                  < value > Bean In Parent </ value >
13              </ constructor-arg >
14       </ bean >
15  </ beans >  

 1  beans.xml
 2 
 3  <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd"
>
 4  < beans >
 5       <!--  hierarchical bean factories  -->
 6       < bean  id ="target1"  class ="SimpleTarget" >
 7           < property  name ="val" >
 8               < ref  bean ="injectBeanParent" />
 9           </ property >
10       </ bean >
11      
12       < bean  id ="target2"  class ="SimpleTarget" >
13           < property  name ="val" >
14               < ref  local ="injectBean" />
15           </ property >
16       </ bean >
17      
18       < bean  id ="target3"  class ="SimpleTarget" >
19           < property  name ="val" >
20               < ref  parent ="injectBean" />
21           </ property >
22       </ bean >
23      
24       < bean  id ="injectBean"  class ="java.lang.String" >
25              < constructor-arg >
26                  < value > Bean In Child </ value >
27              </ constructor-arg >
28       </ bean >
29  </ beans >
30 

SimpleTarget中只有一属性String val


 1  public   static   void  main(String[] args) {
 2          BeanFactory parent  =   new  XmlBeanFactory( new  FileSystemResource(
 3                   " build/parent.xml " ));
 4          BeanFactory child  =   new  XmlBeanFactory( new  FileSystemResource(
 5                   " build/beans.xml " ), parent);
 6 
 7          SimpleTarget target1  =  (SimpleTarget) child.getBean( " target1 " );
 8          SimpleTarget target2  =  (SimpleTarget) child.getBean( " target2 " );
 9          SimpleTarget target3  =  (SimpleTarget) child.getBean( " target3 " );
10 
11          System.out.println(target1.getVal());
12          System.out.println(target2.getVal());
13          System.out.println(target3.getVal());
14      }

运行结果:
Bean In Parent
Bean In Child
Bean In Parent

分析过程:
在bean factory被加载过程中分别加载各层Bean Factory,
BeanFactory parent  =  new  XmlBeanFactory( new  FileSystemResource( " build/parent.xml " ));
BeanFactory child  =  new  XmlBeanFactory( new  FileSystemResource( " build/beans.xml " ), parent);
其中parent参数指定了bean factory间的父子关系。

分析结果:
第一行结果为在父factory中定义的参数,说明在子fantory中可以直接调用父fantory元素,类似于java中的继承关系。
第二行结果说明
< ref  local ="injectBean" />中local指向本fantory元素。
第三行结果为Bean In Parent,说明
< ref  parent ="injectBean" />中parent指向父fantory中元素。 

 

你可能感兴趣的:(Spring笔记之五(分层 Bean Factory 使用))