2008-04-21
反向控制/依赖注射两种主要形式
反向控制/依赖注射两种主要形式
1、基于setter的依赖注射,是在调用无参的构造函数或者无参的静态工程方法实例化你的bean之后,通过调用你的bean上的setter方法实现的。(推荐)
<bean id="exampleBean" class="example.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnthorBean"/></property>
<property name="intergerProperty"><value>1</value></property>
</bean>
<bean id="anotherExampleBean" class="example.AnotherBean">
<bean id="yetAnoterBean" class="example.YetAntherBean">
public class ExampleBean{
private AntherBean beanOne;
private YetAntherBean beanTwo;
private int i;
public void setBeanOne(AntherBean beanOne){
this.beanOne = beanOne;
}
public void setBeanTwo(YetAntherBean beanTwo){
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i){
this.i = i
}
}
见P20 spring-reference.pdf
2、基于构造函数的依赖注射,它时通过调用带有许多参数的构造方法实现的,每个参数表示一个合作者或者属性。
<bean id="exampleBean" class="example.ExampleBean">
<constructor-arg><ref bean="antherExampleBean"/></constructor=arg>
<constructor-arg><ref bean="yetAntherBean"/></constructor=arg>
<constructor-arg><value>1</value></constructor=arg>
</bean>
<bean id="antherExampleBean" class="examle.AntherBean"/>
<bean id="yetAntherBean" class="example.YetAntherBean"/>
public class ExamleBean{
private AntherBean beanOne;
private YetAntoherBean beanTwo;
private int i;
public ExamleBean(AntherBean antherBean,YetAntherBean yetAntherBean,int i){
this.beanOne = antherBean;
this.beanTwo = yetAntherBean;
this.i = i;
}
}
发表评论
- 浏览: 24247 次
- 性别:

- 来自: 安徽

- 详细资料
搜索本博客
我的相册
s1125472579
共 14 张
共 14 张
最近加入圈子
最新评论
-
遍历Set
lz你文不对题啊,你写的明明是遍历hashmap,怎么题目是遍历set,难道是这 ...
-- by Struts_Spring -
MMORPG
...
-- by kayo -
几种java开源缓存系统的性 ...
...
-- by kayo -
[原创]利用IOC的概念改 ...
请你阅读这篇文章,也许对你有点作用。http://kayo.javaeye.co ...
-- by kayo -
[原创]利用IOC的概念改 ...
你好,向你请教关于java的数据采集技术,不知道怎么做,希望你能给例子,谢谢。 ...
-- by looxiaohu






评论排行榜