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. 怎样改动SharePoint管理中心的语言

    在安装了语言包之后,创建站点集的时候,就能够选择语言了. 可是SharePoint管理中心的语言没有变.这个时候.怎么才干让管理中心也使用新的语言呢? 能够依照下面方法. 首先去https://msd ...

  2. POJ Cow Exhibition

    题目链接:Click Here~ 题目意思自己看吧. 算法分析: 对我来想是没有想到,最后看别人的博客才知道的.要把当中的一个条件当作体积.由于两个条件都存在负数,所以还要先保证最后不会再体积中出现负 ...

  3. 在ASP.NET MVC中支持 HttpHandler

    写HttpHandler与ASP.NET WebForm基本没有什么不同.只是部署的时候需要注意,只在Web.config中注册还不行,需要在Global.asax.cs添加一条ignore规则: p ...

  4. c++ builder xe2 字符串转日期

    TFormatSettings * fmt = new TFormatSettings; fmt->ShortDateFormat = L"yy-mm-dd"; fmt-&g ...

  5. Python MQTT客户端实现

    1.安装paho-mqtt 使用Python Package Index (PyPi) pip install paho-mqtt 使用virtualenv virtualenv paho-mqtt ...

  6. Android加密算法之AES加密和解密实现

    <pre name="code" class="plain"><span style="font-family:Microsoft ...

  7. 怎样设置mysql的表不区分你大写和小写

    Linux上安装MySQL默认是数据库的表大写和小写敏感的.改动非常easy.仅仅要该一个mysql的配置文件就能够了. mysql> show tables; +--------------- ...

  8. ucos-ii变量名称表

  9. webpack-dev-server 无法通过ip访问的问题

    使用Vue-cli生成的webpack脚手架,之前一直是可以通过本地ip + 端口来访问的.今天忽然不可以,百度一下才知道缺少了host参数. 打开package.json.在.scripts.dev ...

  10. Atitit.android webview h5运行环境总结

    Atitit.android webview h5运行环境总结 1. WebView 的使用1 2. Js调用java1 3. Js调用java 跟个swt的比较2 3.1. Swt是BrowserF ...