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

  1. AngularJs学习笔记--Dependency Injection(DI,依赖注入)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...

  2. Spring Framework 学习笔记——核心技术之Spring IOC

    Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...

  3. Spring点滴七:Spring中依赖注入(Dependency Injection:DI)

    Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...

  4. [Spring Framework]学习笔记--@Component等stereotype的基础

    在继续讲解Spring MVC之前,需要说一下常用的几个用来标记stereotype的annotation. @Component,@Controller,@Repository,@Service. ...

  5. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

  6. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  7. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  8. Spring MVC 学习笔记一 HelloWorld

    Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...

  9. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

随机推荐

  1. OpenGL 资源汇编

    本文收集和汇总了 OpenGL 的文档.教程和在线书籍,供学习和开发者參考. OPENGL开发教程:http://www.linuxgraphics.cn/opengl/index.html Open ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-TwinCAT自带的找原点功能块MC_Home怎么用

    对于相对编码器类型轴(包括虚拟轴),可以使用贝福提供的找原点功能块MC_Home.   HomingMode是指机器在往前跑的时候(30单位/s的默认速度),当碰到阻挡,则会有一个布尔值从FALSE改 ...

  3. bzoj1710【Usaco2007 Open】Cheappal 便宜回文

    1710: [Usaco2007 Open]Cheappal 便宜回文 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 466  Solved: 262 ...

  4. redislive

    安装Redis Live监控服务 分类: redis 2014-06-25 20:24 436人阅读 评论(0) 收藏 举报 redis redis live为监控redis服务的软件,带有监控web ...

  5. set hive.exec.parallel

    hive.exec.parallel参数控制在同一个sql中的不同的job是否可以同时运行,默认为false.下面是对于该参数的测试过程: 测试sql:select r1.a    from (sel ...

  6. mysql 应用场景

    一.按时间点来统计 ), date_FORMAT(date_Field,'%Y-%m-%d %H:00:00') as dateStr from table_name group by dateStr

  7. Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package

    Atitit.软件命名空间  包的命名统计 及命名表(2000个名称) 方案java package 1. 统计的lib jar 列表1 2. Code3 3. 常用包名按找字母排序(2000个)4 ...

  8. Xilinx下载方式(具体可以参考配置MCS文件时右下角help调出的doc)

    1.两者都属高速并行配置模式.SelectMAP是早期的FPGA两类配置模式之一,是相对于串行(Serial)配置而言的,与主串(Master Serial)和从串(Slave Serial)模式对应 ...

  9. 解决js下跳转无referer的方法

    HTTP Header referer这玩意主要是告诉人们我是从哪儿来的,就是告诉人家我是从哪个页面过来的,可以用于统计访问本网站的用户来源,也可以用来防盗链.获取这个东西最好的方式是js,如果在服务 ...

  10. WCF 动态调用(1)

    很多时候,服务地址都不止一个的,这个时候就要动态去配置地址.配置Web.config,很麻烦 下面就看看怎样实现动态调用WCF. using System; using System.Collecti ...