1. 通过构造函数实现DI

简单类型实例

package examples;public class ExampleBean {// Number of years to calculate the Ultimate Answerprivate int years;// The Answer to Life, the Universe, and Everythingprivate 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 constructorprivate 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的方式好了。

转载于:https://www.cnblogs.com/lemonbar/p/3896863.html

[Spring Framework]学习笔记--Dependency injection(DI)相关推荐

  1. Spring.NET学习笔记——前言

    Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入.面向方面编程(AOP).数据访问抽象及ASP.NET扩展等等.Sprin ...

  2. Spring个人学习笔记

    一.Spring 1.简介 Spring:春天-->给软件行业带来了春天! 2002年:首次推出了Spring框架的雏形:interface21框架. Spring框架即以interface21 ...

  3. Spring Cloud 学习笔记(2 / 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...

  4. Spring Cloud 学习笔记(2 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...

  5. Spring框架学习笔记(三)(AOP,事务管理)

    Spring框架学习笔记(三) 九.AOP 9.1 AOP的注解配置 (1) 新建计算器核心功能(模拟:不能在改动核心代码) (2) 建立一个普通的Java类写增强代码(面向切面编程),使用Sprin ...

  6. Spring Cloud 学习笔记(3 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...

  7. Spring Cloud 学习笔记(3 / 3)

    Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...

  8. Spring Cloud 学习笔记(四)-Spring Cloud Hystrix

    Spring Cloud 学习笔记(四)-Spring Cloud Hystrix 由于前一阵子项目的原因,今天才继续弄上,今天想学习一下Hystrix组件 这个组件还挺抽象的,最开始我一直没太明白, ...

  9. Spring Boot学习笔记-基础(2)

    Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...

最新文章

  1. 一个简单的动态内表alv案例
  2. 第三次学JAVA再学不好就吃翔(part51)--String类的转换功能
  3. php程序layer,php 提交表单 关闭layer弹窗iframe的实例讲解
  4. 大学c语言程序设计大赛,关于举办宁夏大学第二届C语言程序设计大赛的通知
  5. oracle光标位置无效,解决在Form表单中光标移动不了问题
  6. android中的多媒体应用camera
  7. javascript的阻止默认事件和阻止冒泡事件
  8. pythontransform详解_Python自定义聚合函数merge与transform区别详解
  9. 漫画:什么是二叉堆?
  10. js获取post请求参数_SpringMVC请求参数获取时,有这六种方式值得学习
  11. win32,使用PDFlib生成PDF文件
  12. FreeFileSync 免费文件同步软件 实时自动备份重要资料
  13. 第二讲:高性能计算关键技术和趋势分析
  14. 域名注册及免费空间and企业邮箱
  15. Android的Scroller介绍
  16. HDU 6608:Fansblog(威尔逊定理)
  17. Windows10安装MinGW-w64
  18. CF633C Spy Syndrome 2(字典树+dp)
  19. 写在冬日的第一天--一个女程序员第十七年工作总结
  20. BZOJ 1085 骑士精神

热门文章

  1. Apollo配置灰度发布
  2. Hadoop大数据——MR程序map任务数的规划机制
  3. Play! Framework 系列(二):play 的项目结构
  4. springmvc @PathVariable注解进行传参操作
  5. Spring Data ElasticSearch入门案例
  6. JTextPane设置颜色出现的问题
  7. com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed
  8. 思考一个问题:如何重现主从备份失败的案例
  9. visio2013画图时两条直线交叉 如何让它不弯曲
  10. java nio connect_Java NIO系列教程(八) SocketChannel