转贴处 http://www.blogjava.net/improviser/archive/2007/09/25/147934.html
本文研究Spring分层Bean Factory用法,先看有关代码:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1 parent.xml
2
3<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
4<beans>
5<beanid="injectBean"class="java.lang.String">
6<constructor-arg>
7<value>BeanInParent</value>
8</constructor-arg>
9</bean>
10<beanid="injectBeanParent"class="java.lang.String">
11<constructor-arg>
12<value>BeanInParent</value>
13</constructor-arg>
14</bean>
15</beans>
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1beans.xml
2
3<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
4<beans>
5<!--hierarchicalbeanfactories-->
6<beanid="target1"class="SimpleTarget">
7<propertyname="val">
8<refbean="injectBeanParent"/>
9</property>
10</bean>
11
12<beanid="target2"class="SimpleTarget">
13<propertyname="val">
14<reflocal="injectBean"/>
15</property>
16</bean>
17
18<beanid="target3"class="SimpleTarget">
19<propertyname="val">
20<refparent="injectBean"/>
21</property>
22</bean>
23
24<beanid="injectBean"class="java.lang.String">
25<constructor-arg>
26<value>BeanInChild</value>
27</constructor-arg>
28</bean>
29</beans>
30
SimpleTarget中只有一属性String val
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1publicstaticvoidmain(String[]args){
2BeanFactoryparent=newXmlBeanFactory(newFileSystemResource(
3"build/parent.xml"));
4BeanFactorychild=newXmlBeanFactory(newFileSystemResource(
5"build/beans.xml"),parent);
6
7SimpleTargettarget1=(SimpleTarget)child.getBean("target1");
8SimpleTargettarget2=(SimpleTarget)child.getBean("target2");
9SimpleTargettarget3=(SimpleTarget)child.getBean("target3");
10
11System.out.println(target1.getVal());
12System.out.println(target2.getVal());
13System.out.println(target3.getVal());
14}
运行结果:
Bean In Parent
Bean In Child
Bean In Parent
分析过程:
在bean factory被加载过程中分别加载各层Bean Factory,BeanFactoryparent=newXmlBeanFactory(newFileSystemResource("build/parent.xml"));
BeanFactorychild=newXmlBeanFactory(newFileSystemResource("build/beans.xml"),parent);
其中parent参数指定了bean factory间的父子关系。
分析结果:
第一行结果为在父factory中定义的参数,说明在子fantory中可以直接调用父fantory元素,类似于java中的继承关系。
第二行结果说明 <reflocal="injectBean"/>中local指向本fantory元素。
第三行结果为Bean In Parent,说明<refparent="injectBean"/>中parent指向父fantory中元素。
分享到:
相关推荐
hierarchical guanyu R
Easy to use flat one level state machine for simple use cases.Hierarchical state machine structure to ease complex state configuration.State machine regions to provide even more complex state ...
Hierarchical Dirichlet Process简单介绍
论文摘要:元学习允许智能代理利用先前的学习情节作为快速提高新任务性能的基础。贝叶斯分层建模提供了一个理论框架,用于形式化元学习,作为对跨任务共享的一组参数的推断。在这里,我们重新定义了Finn等人的模型不...
Hierarchical Dirichlet Proces HDP 代码 matlab
Hierarchical Clustering And Structuring Of Data With Self-Organizing
在Oracle中,有个很方便的特性,层次关系子句。它可以实现需要递归关联查询的各种情景,Oracle_hierarchical_SQL_查询应用.doc里面给出了几个情景,用详细的sql语句来说明怎么查询出需要的组合信息
Knowledge Graph Embedding with Hierarchical Relation Structure 阅读报告
多层分类Hierarchical softmax模型,王圣,周延泉,本文引入了一个新的结构来进行多层分类,它是一个多层分类Hierarchical softmax模型。以往的多层分类问题不能用一个Hierarchical softmax模型��
Hierarchical Graph Models for Conflict Resolution 冲突消解的层次图模型
Behavior Trees for Hierarchical RTS AI
Chloroplast thylakoid membranes accommodate densely packed protein complexes in ordered, often semi- crystalline arrays and are assembled into highly organized multilamellar systems, an organization ...
MartinPelikan HierarchicalBayesian Optimization Algorithm Towarda New Generation of Evolutionary Algorithms
develops a useful correspondence between any hierarchical system of such clusters, and a particular type of distance measure. The correspondence gives rise to two methods of clustering that are ...
关于层次聚类(hierarchical clustering)的基本步骤: 1、假设每个样本为一类,计算每个类的距离,也就是相似度 2、把最近的两个合为一新类,这样类别数量就少了一个 3、重新新类与各个旧类(去了那两个合并的类)之间...
Bayesian Disease Mapping - Hierarchical Modeling in Spatial Epidemiology
A Hierarchical Neural Autoencoder for Paragraphs and Documents
VulExplainer A Transformer-Based Hierarchical Distillation的笔记
managing-hierarchical-data-in-mysql.rar
hierarchical clustering