[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 中 前端用户登录时携带当 ...
随机推荐
- git学习——分支
分支 创建分支:git branch 如:git branch testing Git通过HEAD指针知道用户是在哪一个分支上工作. 切换分支用git checkout命令,注意:可以用git sta ...
- Laravel 学习 .env文件 getenv 获得环境变量的值
Laravel 学习 .env文件 getenv 获得环境变量的值 我们还需要对应用的 .env 文件进行设置,为应用指定数据库名称 sample. .env . . . DB_DATABASE=s ...
- 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)
摘要 MySQL的最初的核心思想,主要是开源.简便易用.其开发可追溯至1985年,而第一个内部发行版本号诞生,已经是1995年. 到1998年,MySQL已经能够支持10中操作系统了.当中就包含win ...
- XPage的使用示范例子
代码地址如下:http://www.demodashi.com/demo/12975.html XPage [[api][apisvg]][api] 一个非常方便的fragment页面框架 特点 支持 ...
- 利用pandas进行数据分析之ndarray结构
Numpy的重要特点就是其N维数组对象, 1.ndarray每个元素是相同的,每个数组都有一个两个对象: .shape:用于表示维度大小的元组 .dtype:用户表示数组类型的对象 2.创建数组 ar ...
- Spring AOP事务管理(使用切面把事务管理起来)
在<Spring Transaction 分析事务属性(事务的基本概念.配置)>基础上 http://blog.csdn.net/partner4java/article/details/ ...
- weka入门
每天都必须记录自己的一点一滴,不记录下来,过一段时间就会忘记,忘记了就等于没有经历过. 我不能每天这样浑浑噩噩的过下去.我要有计划,有梦想.追求卓越.成就不同. 我今天開始我的学习weka之路.学的时 ...
- nginx 404重定向到自定义页面
在访问时遇到上面这样的404错误页面,我想99%(未经调查,估计数据)的用户会把页面关掉,用户就这样悄悄的流失了.如果此时能有一个漂亮的页面能够引导用户去他想去的地方必然可以留住用户.因此,每一个网站 ...
- 使用 %matplotlib inline 出错?
%matplotlib inline 是一个魔法函数(Magic Functions).官方给出的定义是:IPython有一组预先定义好的所谓的魔法函数(Magic Functions),你可以通过命 ...
- 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用
还原 AdventureWorks Sample DataBase 时遇到 FILESTREAM feature is disabled 错误提示. FileStream是SQL Server 200 ...