[Spring Framework]学习笔记--Dependency injection(DI)
1. 通过构造函数实现DI
简单类型实例
package examples; public class ExampleBean { // Number of years to calculate the Ultimate Answer
private int years; // The Answer to Life, the Universe, and Everything
private String ultimateAnswer;
//如果不能在debug模式下进行编译,必须要加下面一行。针对下面的方式3,通过参数名字。
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
} }
相应的xml配置为
<bean id="exampleBean" class="examples.ExampleBean"> //方式1,通过类型,如果存在两个参数同类型的话,不行。
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/> //方式2,通过参数位置。
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
//方式3,通过参数名字。
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateAnswer" value="42"/>
</bean>
对象类型实例
package x.y; public class Foo { public Foo(Bar bar, Baz baz) {
// ...
} }
xml配置
<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>
参数也可以像下面这样指定
<constructor-arg>
<ref bean="bar"/>
</constructor-arg>
如果是通过工厂模式,可以采用下面的方式
<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;
} }
2. 通过set方法来实现DI
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested ref element -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property> <!-- setter injection using the neater ref attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
注:property的name是和set方法中的名字一致的。
public class ExampleBean { private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i; public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
} public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
} public void setIntegerProperty(int i) {
this.i = i;
} }
总结:在我们日常的工程中,上面两种方式如何使用呢?
可以从需要来看,如果这个属性是必须的,那就放在构造函数中;如果是可选的,那就用set的方式好了。
[Spring Framework]学习笔记--Dependency injection(DI)的更多相关文章
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
- Spring Framework 学习笔记——核心技术之Spring IOC
Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...
- Spring点滴七:Spring中依赖注入(Dependency Injection:DI)
Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...
- [Spring Framework]学习笔记--@Component等stereotype的基础
在继续讲解Spring MVC之前,需要说一下常用的几个用来标记stereotype的annotation. @Component,@Controller,@Repository,@Service. ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...
- 【转】Spring.NET学习笔记——目录
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
随机推荐
- InputStream写文件出现大量NUL
写文件大家出现最多的是汉字知码之类的问题,今天不是乱码问题,是出现在大量空字符,用记事本打开是不可见的.如果用NodePad++打开则会显示NUL 问题分题: 刚开始以为是编码问题,试了几个编码发现问 ...
- lucene 查询
csdn blog - Lucene 3.0 的Query Parser(查询语法) ibm developerWorks - 使用 Apache Lucene 2.4.1 搜索文本 osch ...
- IIS如何添加m3u8流媒体类型
m3u8,mime类型填写: application/x-mpegURL. ts ,mime类型填写: video/MP2T
- java中ThreadLocalRandom类和Random类的使用
package frank; import java.lang.*; import java.util.*;//工具类一般都在util里面 import java.util.concurrent.Th ...
- angularjs 可以加入html标签方法------ng-bind-html的用法总结(1)
本篇主要讲解angular中的$sanitize这个服务.此服务依赖于ngSanitize模块.(这个模块需要加载angular-sanitize.js插件) 要学习这个服务,先要了解另一个指令: n ...
- python c example2:pylame2
#include <Python.h> #include <lame.h> //pyton object variables typedef struct{ PyObject_ ...
- View拖拽 自定义绑定view拖拽的工具类
由于工作需求,需要用到这种处理方法所以我就写了这个 废话不多说先看效果图 接下来就看代码吧 DragDropManager import android.app.Activity; import an ...
- 【转】Monkey测试2——Monkey测试策略
Monkey的测试策略 一. 分类 Monkey测试针对不同的对象和不同的目的采用不同的测试方案,首先测试的对象.目的及类型如下: 测试的类型分为:应用程序的稳定性测试和压力测试 测试对象分为:单一a ...
- Xshell配色方案(Solarized Dark)
将以下内容复制并保存到文件中,文件名以xc为后缀,如:Solarized Dark.xcs [Solarized Dark] text= cyan(bold)=93a1a1 text(bold)= m ...
- python 面向对象类成员(字段 方法 属性)
一.字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段属于对象 静态字段属于类 class Province: # 静态字段 countr ...