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的更多相关文章

  1. 4. svg学习笔记-文档结构元素和样式的使用

    svg除了绘图元素之外还有一部分是专门用于文档结构的,这类元素有<g>,<use>,<defs>,<symbol>等 <g>元素 如果我们仅 ...

  2. MongoDB学习笔记——文档操作之查询

    查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...

  3. MongoDB学习笔记——文档操作之增删改

    插入文档 使用db.COLLECTION_NAME.insert() 或 db.COLLECTION_NAME.save() 方法向集合中插入文档 db.users.insert( { user_id ...

  4. StyleCop学习笔记-文档规则

    文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...

  5. Spring3.0官网文档学习笔记(一)

    Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...

  6. Spring3.0官网文档学习笔记(七)--3.4.2

    3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...

  7. Spring3.0官网文档学习笔记(二)

    1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包)     spring-*.jar *号代表 ...

  8. Spring3.0官网文档学习笔记(四)--3.1~3.2.3

    3.1 Spring IoC容器与Beans简单介绍     BeanFactory接口提供对随意对象的配置:     ApplicationContext是BeanFactory的子接口.整合了Sp ...

  9. Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

    3.4.3 使用depends-on     使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...

随机推荐

  1. 解决wordpress发表文章,照片不能居中的问题

    最近,随着一个年轻漂亮的女人的帮助(简直美极了.图相当火爆,性格非常好.大家闺秀型,照片给大家看看下一个突发.哈哈)获取她的个人博客,地址似乎是www.okaaok.com遇到发表文章.照片不能反正水 ...

  2. LeetCode 53 Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. 使用require.js和backbone实现简单单页应用实践

    前言 最近的任务是重做公司的触屏版,于是再园子里各种逛,想找个合适的框架做成Web App.看到了叶大(http://www.cnblogs.com/yexiaochai/)对backbone的描述和 ...

  4. Eclipse+Maven创建webapp项目<一> (转)

    Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...

  5. python正文(两)

    在本文中,我读了记录和总结<Python标准库>一本书,本节课文的学习和理解. 事实上,在Python于,使用一些方法这段文字是一回事,尤其是经常使用.在一般情况下,会用String这样的 ...

  6. Android应用-包装脚本批量方法

    1. 设定ant周边环境 加入用户变量: 变量名:ANDROID_SDK_ROOT 变量值:D:\Android Develop\adt-bundle-windows-x86_64-20140321\ ...

  7. 我见过最好的vsftpd配置教程(转)

    环境:CentOS 5.0 操作系统一.安装:1.安装Vsftpd服务相关部件:[root@KcentOS5 ~]# yum install vsftpd*Dependencies Resolved= ...

  8. node.js基础:数据存储

    无服务器的数据存储 内存存储 var http = require('http'); var count = 0; //服务器访问次数存储在内存中 http.createServer(function ...

  9. Android - 支持不同的设备

    世界上有各种不一样形状和大小的Android设备.由于有各种不同类型的设备,你的app可能在各种设备上运行.为了在Android上尽可能的成功,你的app需要使配各种不同的设备.最重要的几点需要考虑的 ...

  10. zabbix 实现curl 显示器

    1.进入Configure->Templates 2. 新建一个模板 3.新建模板,并保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFpND ...