依赖注入(Dependency Injection,简称DI)意思是由容器或者框架将被调用类注入给调用对象,以此来降低调用对象和被调用类之间的依赖关系。

依赖注入主要有2种不同的实现形式:
1. 构造函数注入(Constructor Injection)
2. 设值注入(Setter Injection)

1.构造函数注入
通过调用类的构造函数,并将被调用类当做参数传递给构造函数,以此实现注入。

example:

public class UserImpl implements UserDao {private String name;private String password;public UserImpl(String name) {this.name = name;}public UserImpl(String name, String password) {this.name = name;this.password = password;}@Overridepublic String toString() {return "username="+name+"   password="+password;}@Overridepublic void getUser() {System.out.println(toString());}}
public class UserServiceImpl implements UserService {private UserDao userDao;public UserServiceImpl(UserDao userDao) {this.userDao = userDao;}public void getUser() {userDao.getUser();}
}

xml配置文件的具体配置

     <!--采用有参数的构造方法 userImpl是实现了userdao的实现类--><bean id="user" class="com.jsu.dao.Impl.UserImpl"><constructor-arg index="0" value="zhangsan"/><constructor-arg index="1" value="123"/></bean><bean id="service1" class="com.jsu.service.serviceImpl.UserServiceImpl"><property name="userDao" ref="user"/></bean>

上述的例子中service指定userImpl这个对象为UserDao,并将其注入给UserServiceImpl这个类。最终由UserDao这个对象完成getUser的功能。

2.设值注入
设值注入主要通过添加并使用被调用类的set的方法来完成注入。
example:

public class UserServiceImpl implements UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void getUser() {userDao.getUser();}
}

xml配置信息与构造函数注入相同。但是上述的例子确实通过set方法将userdao对象注入给UserServiceImpl类。

注意二者的区别

使用构造函数注入可以隐藏注入字段的信息,
使用设置注入需要明确指出注入字段的信息。使用构造函数注入由参数下标决定注入的参数
使用设置注入则需要指明每个参数的名称。当参数较多时候使用设值注入则更加容易理解

两种注入方式的选择

Constructor-based or setter-based DI?Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Required annotation on a setter method can be used to make the property a required dependency.The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection.Use the DI style that makes the most sense for a particular class. Sometimes, when dealing with third-party classes for which you do not have the source, the choice is made for you. For example, if a third-party class does not expose any setter methods, then constructor injection may be the only available form of DI.

如果需要了解更具体的set注入方式 可参照官方文档:

Spring DI[依赖注入]相关推荐

  1. Spring DI依赖注入讲解

    DI:dependency injection 依赖注入 在spring框架负责创建Bean对象时,动态将依赖对象注入到Bean组件. public class UserServiceImpl imp ...

  2. Spring DI(依赖注入)

    DI依赖注入 IoC(Inversion Of Control)控制翻转,Spring反向控制应用程序所需要使用的外部资源 DI(Dependency Injection)依赖注入,应用程序运行依赖的 ...

  3. Spring DI(依赖注入)注解篇

    1 课程内容介绍 我之前写的的一篇博客Spring核心功能DI(依赖注入)xml篇主要介绍了如何通过配置xml的方式来实现依赖注入,今天我们来介绍如何通过注解方式完成我们的依赖注入操作. 2 注入基本 ...

  4. Spring DI(依赖注入)Xml篇

    1 DI(依赖注入)简单介绍 如果您对Spring了解甚少,建议先移步我的另一篇博客Spring核心功能IOC之HelloWorld因为下面的内容是在该文章基础上进行阐述的 .我们可以通过一段简单代码 ...

  5. 手写Spring DI依赖注入,嘿,你的益达!

    手写DI 提前实例化单例Bean DI分析 DI的实现 构造参数依赖 一:定义分析 二:定义一个类BeanReference 三:BeanDefinition接口及其实现类 四:DefaultBean ...

  6. Spring DI依赖注入方式

    1.构造器注入 2.Set方式注入[重点] 依赖注入:Set注入 依赖:bean对象的创建依赖于容器 注入:bean对象中的所有属性,由容器来注入. [环境搭建] Student package co ...

  7. Spring DI(依赖注入)构造器注入篇

    Spring 在不使用自动装配的方式进行注入需要我们必须为成员属性提供setter方法,这种方式相对比较繁琐,除了setter方法注入方式外,Spring还为我们提供了构造器配置的注入方式. 构造器默 ...

  8. 【Java从0到架构师】Spring - IoC 控制反转、DI 依赖注入

    IoC 控制反转.DI 依赖注入 Spring 简介 Spring 基本使用 - IoC 容器 依赖注入 (Dependency Injection) 基于 setter 的注入 - 自定义对象 be ...

  9. Spring(二)--------Spring配置、DI依赖注入、Bean自动装配

    Spring(二)--------Spring配置.DI依赖注入.Bean自动装配 5.Spring配置 5.1 别名 设置别名:第一种方式alias <!--其中name为ID的对应值--&g ...

最新文章

  1. 我终于决定要放弃 okhttp、httpClient,选择了这个牛逼的神仙工具!贼爽!
  2. ios 静态库合成_iOS链接原理解析与应用实践
  3. UNIX网络编程——TCP/IP简介
  4. cjuiautocomplete ajax,$ _GET( '术语' 在CJuiAutocomplete的Widget
  5. [Cake] 2. dotnet 全局工具 cake
  6. 淘宝JavaScript 编码风格规范
  7. TVM:使用 Schedule 模板和 AutoTVM 来优化算子
  8. 设计模式 - 单例模式(Singleton Pattern)
  9. 陈纪修老师《数学分析》 第02章:数列极限 笔记
  10. 推荐一本好书《代码整洁之道 (claen code) 》
  11. 滴滴的焦虑,从未根治!
  12. dex字符串解密_[原创]通过CTF学习Android漏洞(炸弹引爆+dex修复)
  13. 原生js的e.target.closest()方法
  14. KILE 报 contains an incorrect path. 错误
  15. Android 融云单聊与群聊消息免打扰功能设置与消息删除功能实现
  16. 第六讲 幂级数的收敛半径和收敛域
  17. ingress 七层负载均衡器
  18. (c语言详解)07-图6 旅游规划(详细解释)
  19. Python+Django实现基于人脸识别的门禁管理系统,附带源码!!
  20. 1.MySQL数据库 2.SQL语句

热门文章

  1. spring MVC使用自定义的参数解析器解析参数
  2. encoding - 如何将 Dart 的ByteData转换为字符串?
  3. vscode如何及时提示flutter代码
  4. spark python3.6_在mac上搭建spark+ipython环境
  5. java实现下载压缩文件_java实现文件压缩下载----压缩下载zip
  6. 新国货美妆品牌数字营销能力升级“三步法”
  7. 神策数据上线 IPTV Demo ,三大价值助力数据驱动
  8. 人文英语学习品牌「友邻优课」携手神策数据 精细化数据分析让每一次互动都有价值
  9. 技术内参 | 数据分析,如何解决精度丢失的问题?
  10. Monkey Server自动化脚本