依赖注入的原理
依赖注入的方式---XML配置
依赖注入的方式---注解的方式

Spring 它的核心就是IOC和AOP。而IOC中实现Bean注入的实现方式之一就是DI(依赖注入)。

一 DI的原理
DI的基本原理:对象之间的依赖关系只会通过三种方式:构造函数参数,工厂方法的参数以及构造函数或工厂方法创建的对象属性设置。因此,容器的工作哦就是
在创建Bean时注入所有的依赖关系。相对于由Bean自己控制其实例化,直接在构造器中指定依赖关系或者类似服务定位器(Service
Locator)这三种自主控制依赖关系注入的方法,而控制权从根本上发生改变,即控制反转(Inverse of Controll)---IOC.

应用DI规则后,我们不用在关注对象之间的依赖关系,从而达到高层次的松耦合。DI有两种实现方式---Setter/getter方式(传值方式)和构造器方式(引用方式)。

下面就从XML配置和注解角度来介绍这两种方式。

二、DI的方式---XML配置
1. Setter/getter方法
下面是一个Sample

1)、beans.xml (外部注入的方式)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
  9. <bean id="personService" class="com.spring.service.impl.PersonServiceBean" >
  10. <property name="personDao" ref="personDao"/>
  11. </bean>
  12. </beans>

2) PersonDao.java

  1. package com.spring.dao;
  2. ublic interface PersonDao {
  3. public void add();

3) PersonDaoImpl.java

  1. package com.spring.dao.impl;
  2. import com.spring.dao.PersonDao;
  3. public class PersonDaoImpl implements PersonDao {
  4. public void add() {
  5. System.out.println("PersonDao.add() is running.");
  6. }
  7. }

4) PersonService.java

  1. package com.spring.service;
  2. public interface PersonService {
  3. public  void save();
  4. }

5) PersonServiceBean.java

  1. package com.spring.service.impl;
  2. import com.spring.dao.*;
  3. import com.spring.service.PersonService;
  4. import javax.annotation.PostConstruct;
  5. import javax.annotation.PreDestroy;
  6. public class PersonServiceBean implements PersonService {
  7. private PersonDao personDao;
  8. public PersonServiceBean(){
  9. System.out.println("personServiceBean.constructor() is running.");
  10. }
  11. public PersonDao getPersonDao() {
  12. return personDao;
  13. }
  14. public void setPersonDao(PersonDao personDao) {
  15. this.personDao = personDao;
  16. }
  17. public void save(){
  18. System.out.println("Name:" );
  19. personDao.add();
  20. }
  21. @PostConstruct
  22. public void init(){
  23. System.out.println("PersonServiceBean.init() is running.");
  24. }
  25. @PreDestroy
  26. public void destory(){
  27. System.out.println("PersonServiceBean.destory() is running.");
  28. }
  29. }

6) SpringIOCTest.java(测试类)

  1. package junit.test;
  2. import org.junit.BeforeClass;
  3. import org.junit.Test;
  4. import org.springframework.context.support.AbstractApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import com.spring.service.PersonService;
  7. public class SpringIOCTest {
  8. @BeforeClass
  9. public static void setUpBeforeClass() throws Exception {
  10. }
  11. @Test public void instanceSpring(){
  12. AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  13. PersonService personService = (PersonService)context.getBean("personService");
  14. personService.save();
  15. //          context.close();
  16. }
  17. }

我们还可以采用内部注入的方式来处理:
Beans.xml修改如下:

  1. <bean id="personService">
  2. <property name="personDao">
  3. <bean/>
  4. </property>
  5. </bean>

2、构造器注入

以下是个例子

  1. <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
  2. <bean id="personService"  class="com.spring.service.impl.PersonServiceBean" init-method="init" destroy-method="destory">
  3. <constructor-arg index="0" type="com.spring.dao.PersonDao" ref="personDao"/>
  4. <constructor-arg index="1" value="Jamson" />
  5. </bean>

2) PersonService.java:同Setter方法注入
3) PersonServiceBean.java

  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. private String name;
  4. public PersonServiceBean(){
  5. System.out.println("personServiceBean.constructor() is running.");
  6. }
  7. public PersonServiceBean(PersonDao personDao, String name) {
  8. this.personDao = personDao;
  9. this.name = name;
  10. }
  11. //      public PersonDao getPersonDao() {
  12. //          return personDao;
  13. //      }
  14. //     public void setPersonDao(PersonDao personDao) {
  15. //         this.personDao = personDao;
  16. //      }
  17. public void save(){
  18. System.out.println("Name:"+name );
  19. personDao.add();
  20. }
  21. @PostConstruct
  22. public void init(){
  23. System.out.println("PersonServiceBean.init() is running.");
  24. }
  25. @PreDestroy
  26. public void destory(){
  27. System.out.println("PersonServiceBean.destory() is running.");
  28. }
  29. }

4) PersonDao.java:同Setter方法注入
5) PersonDaoImpl.java:同Setter方法注入
6) 测试类(SpringIOCTest.java):同上

控制台信息

  1. PersonServiceBean.init() is running.
  2. Name:Jamson
  3. PersonDao.add() is running.
  4. PersonServiceBean.destory() is running.

三 DI的方式---注解的配置
自从Jdk5中引入Annotation类后,在EJB和Spring中得到广泛的使用,也越来越被开发者们在平时的应用中使用。主要是用在Field上
的注入。在日常的应用开发中,一个项目中有很多的bean,如果使用XML文件配置,就会导致配置文件难以管理。使用注解注入的时候,能够使bean的配
置文件内容不至于繁杂。

首先介绍下注解的两种方式:@Resource(javax.annotation.Resource)和@Autowired.
@Resource:首先按照名称去寻找当前的bean,如果找不到的话,那就以类型装配。
@Autowired:首先按照类型去寻找当前的bean, 如果找不到的话,那就以名称装配。
1. Resource
下面介绍一个Sample:

1)beans.xml

  1. <context:annotation-config/>
  2. <bean id="personDao" class="com.spring.dao.impl.PersonDaoImpl"></bean>
  3. <bean id="personService"  class="com.spring.service.impl.PersonServiceBean"  init-method="init" destroy-method="destory">
  4. </bean>

2) PersonService.java:同上。
3) PersonServiceBean.java

  1. package com.spring.service.impl;
  2. import com.spring.dao.*;
  3. import com.spring.service.PersonService;
  4. import javax.annotation.PostConstruct;
  5. import javax.annotation.PreDestroy;
  6. import javax.annotation.Resource;
  7. public class PersonServiceBean implements PersonService {
  8. @Resource(name="personDao")
  9. private PersonDao personDao;
  10. private String name;
  11. public PersonServiceBean(){
  12. System.out.println("personServiceBean.constructor() is running.");
  13. }
  14. public PersonServiceBean(PersonDao personDao, String name) {
  15. this.personDao = personDao;
  16. this.name = name;
  17. }
  18. //      public PersonDao getPersonDao() {
  19. //          return personDao;
  20. //      }
  21. //     public void setPersonDao(PersonDao personDao) {
  22. //         this.personDao = personDao;
  23. //      }
  24. public void save(){
  25. System.out.println("Name:"+name );
  26. personDao.add();
  27. }
  28. @PostConstruct
  29. public void init(){
  30. System.out.println("PersonServiceBean.init() is running.");
  31. }
  32. @PreDestroy
  33. public void destory(){
  34. System.out.println("PersonServiceBean.destory() is running.");
  35. }
  36. }

4) PersonDao.java:同上
5) PersonDaoService.java:同上
6) 测试类SpringIOCTest.java:同上

Spring IOC 依赖注入的两种方式XML和注解的更多相关文章

  1. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  2. spring的ioc依赖注入的三种方法(xml方式)

    常见的依赖注入方法有三种:构造函数注入.set方法注入.使用P名称空间注入数据.另外说明下注入集合属性 先来说下最常用的那个注入方法吧. 一.set方法注入 顾名思义,就是在类中提供需要注入成员的 s ...

  3. spring的IOC——依赖注入的两种实现类型

    一.构造器注入: 构造器注入,即通过构造函数完成依赖关系的设定.我们看一下spring的配置文件: <constructor-arg ref="userDao4Oracle" ...

  4. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  5. Spring的依赖注入的2种方式(1天时间)

    今天花了一天的时间才调试出来 private      接口   实现类的那个bean; 最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean 就是后面的那个名字写错了,花了整整一天 ...

  6. 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型(四)

    上篇博文,我们介绍了为什么使用IOC容器,和IOC的设计思想以及IOC容器的优缺点,并且给大家转载了一篇介绍IOC原理的博文,我们这篇主要给大家依赖注入的两种方式,以及他们的优缺点. 我们这篇博文还是 ...

  7. ASP.NET MVC中使用Unity进行依赖注入的三种方式

    在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...

  8. Spring中属性注入的几种方式以及复杂属性的注入

    在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...

  9. 峰Spring4学习(2)依赖注入的几种方式

    一.装配一个bean 二.依赖注入的几种方式 com.cy.entity   People.java: package com.cy.entity; public class People { pri ...

随机推荐

  1. 理解RESTful

    REST全称为Representational State Transfer,可以翻译为“表现状态转换”,是由是Roy Thomas Fielding在他2000年的博士论文中提出的,目的是为了得到一 ...

  2. hdu 5596 GTW likes gt

    题目链接: hdu 5596 题意不难懂(虽然我还是看了好久)大概就是说 n 个人排成一列,分成两组, 第 i 秒时第 i 个人会消灭掉前面比他 b[i] 值低的且和他不同组的人,c[i] 表示第 c ...

  3. jq变态全选vs原生变态全选

    <script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...

  4. ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)

    ngui中 代码调用按钮事件 好烦人啊这个问题, 我弄完发上来 这个问题解决了一半 发现可以用 按钮绑定来解决这个问题,并且更安全方便快速 直接在按钮上添加一个 key binding 指定按键 搞定 ...

  5. 【J-meter】变量加密之Bean shell使用

    参考资料: http://www.cnblogs.com/puresoul/p/4915350.html http://www.cnblogs.com/tester-hehehe/p/5466364. ...

  6. [HTML/HTML5]6 使用图像

    6.1  将图像作为Web页面的前景元素 使用img元素,就可以在Web页面中添加图像.img是image的缩写.只需在img元素中添加src(source的缩写)属性,并为该属性设置相应的值,就可以 ...

  7. pip常见操作收录

    由于这些东西比较容易忘掉,在这里几下吧 1. pip 对应用进行安装: sudo pip install  your_app 2. pip 对应用进行update sudo pip install   ...

  8. vue的transition过渡效果

    需要4个类: *-enter: 进入的开始状态, *-enter-active: 进入的结束状态, *-leave: 离开的开始状态, *-leave-active: 离开的结束状态 vue-rout ...

  9. php curl用法

    curl 是使用URL语法的传送文件工具,支持FTP.FTPS.HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP.curl 支持SSL证书.HTTP POS ...

  10. java的前台与后台

    技术上:前台是指web展示,webservice接口等输入输出接口,后台是指支持这些接口的程序. 例如读写数据库,读写文件,业务逻辑处理. 业务上来讲:前台是提供给最终用户使用的界面,后台是指管理使用 ...