Spring3.0学习笔记文档的官方网站(六)--3.4.1
3.4 依靠
3.4.1 依赖注入
依赖注入两种方式:基于构造函数DI、基于setter方法DI。
3.4.1.1 基于构造函数DI
参数是引进一个对象的。和缺乏父母之前-子类关系:
package x.y; public class Foo { public Foo(Bar bar, Baz baz) {
// ...
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean> <bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/> </beans>
当使用简单类型时。spring不好确定value的类型。
package examples; public class ExampleBean { // No. of years to the calculate the Ultimate Answer
private int years; // The Answer to Life, the Universe, and Everything
private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
或者能够使用index来指定參数要依照什么样的顺序设置。
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
对spirng3.0来讲,还能够使用name来指定要设置的属性名。
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>
要让这种方法生效,要记得:your code must be compiled with the debug flag enabled(啥意思?)或者要使用@ConstructorProperties:
package examples; public class ExampleBean { // Fields omitted @ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
3.4.1.2 基于setter方法的DI
3.4.1.3 依赖解析过程
容器在创建之后。会对每一个bean进行验证,包含验证bean引用的属性是否是有效的。可是bean引用的properties仅仅有在bean创建之后才会被创建。
单例bean在容器被创建的时候就被创建。其它的bean仅仅有在被请求才实用到。
Spring设置properties或者解决依赖尽可能的晚。
A依赖于B,那么B会先被创建,然后才创建A。
3.4.1.4 DI样例
使用静态工厂方法,传參数。
<bean id="exampleBean" class="examples.ExampleBean"
factory-method="createInstance">
<constructor-arg ref="anotherExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean { // a private constructor
private ExampleBean(...) {
...
} // a static factory method; the arguments to this method can be
// considered the dependencies of the bean that is returned,
// regardless of how those arguments are actually used.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { ExampleBean eb = new ExampleBean (...);
// some other operations...
return eb;
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Spring3.0学习笔记文档的官方网站(六)--3.4.1的更多相关文章
- 4. svg学习笔记-文档结构元素和样式的使用
svg除了绘图元素之外还有一部分是专门用于文档结构的,这类元素有<g>,<use>,<defs>,<symbol>等 <g>元素 如果我们仅 ...
- MongoDB学习笔记——文档操作之查询
查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...
- MongoDB学习笔记——文档操作之增删改
插入文档 使用db.COLLECTION_NAME.insert() 或 db.COLLECTION_NAME.save() 方法向集合中插入文档 db.users.insert( { user_id ...
- StyleCop学习笔记-文档规则
文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...
- Spring3.0官网文档学习笔记(一)
Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...
- Spring3.0官网文档学习笔记(七)--3.4.2
3.4.2 依赖与配置的细节 3.4.2.1 Straight values (primitives, Strings, and so on) JavaBeans PropertyE ...
- Spring3.0官网文档学习笔记(二)
1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包) spring-*.jar *号代表 ...
- Spring3.0官网文档学习笔记(四)--3.1~3.2.3
3.1 Spring IoC容器与Beans简单介绍 BeanFactory接口提供对随意对象的配置: ApplicationContext是BeanFactory的子接口.整合了Sp ...
- Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6
3.4.3 使用depends-on 使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...
随机推荐
- 解决alaert.builder二次调用报错的bug
报错的代码是: The specified child already has a parent. You must call removeView() on the child's parent f ...
- HDU 4951 Multiplication table 阅读题
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表.行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i ...
- 【牛刀小试2】password保
]password保 主要知识: 1. while循环 2. do-while循环 3. if-else 4. strcmp()函数 [充电一下 ...
- WinForm 国际化开发一例
1.新建一个WinForm程序(在中文版的Windows下),如下,添加1个Button和1个Label 设置Form1的localizable属性为True(Form1的properties里的De ...
- Java正则表达式例子汇总
1.过滤特殊字符 package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public stati ...
- 解决apache+tomcatserver环境中文乱码的问题
在使用apache做转发服务器时,碰到了中文乱码的问题. 说说解决思路: 1.通常乱码是由于编码不统一造成的.所以要先推断是不是由于编码问题造成的,假设是的话,那统一编码就能够去解决. 2.tomca ...
- Oracle Instanc Client安装命令工具
条件 1.Linux RHEL 6.X X86_64操作系统 2.从安装Oracleserver的server此次收购Oracle相关文件(同OS) 软件下载 从Oracle包: 1) instan ...
- Java 开源博客 —— Solo 0.6.9 发布时间!
Solo 它是 GitHub 上 Star 的最大数量 Java 博客系统,今天,我们宣布 0.6.9 正式版,欢迎来到下载. 特性 基于标签的文章分类 博客/标签 Atom/RSS.Sitemap ...
- Linux鸟哥的私房菜(3)— 总体规划和磁盘分区 读书笔记
1.每个硬件设备Linux中的文件名称 在Linux系统中.每一个设备都被当成一个文件来对待.而且差点儿全部的硬件设备文件都在/dev文件夹下 常见设备与其对于文件名称 2.磁盘连接的方式与设备文件名 ...
- uml学习书籍
uml真正实用的书5这是足够.学习如以下的处理: <UML distilled><--><UML和模式应用>-><UML用户指南> 附加两本&l ...