Spring筆記之五(Hierarchical Bean Factory Usage)
?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>?
?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?
?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?????}
?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中元素。
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2007-09-25 00:05 poetguo 閱讀(1944) 評論(1) 編輯 收藏 所屬分類: Spring