Spring IOC 依赖注入的两种方式XML和注解

来源:互联网 发布:java webstart 编辑:程序博客网 时间:2024/06/07 01:56

依赖注入的原理 
依赖注入的方式---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 (外部注入的方式)

[html] view plaincopy
  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

[html] view plaincopy
  1. package com.spring.dao;   
  2. ublic interface PersonDao {  
  3. public void add();     


 

3) PersonDaoImpl.java

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


4) PersonService.java

[html] view plaincopy
  1. package com.spring.service;   
  2. public interface PersonService {  
  3.  public  void save();     
  4. }  


5) PersonServiceBean.java

[html] view plaincopy
  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.   
  7. public class PersonServiceBean implements PersonService {  
  8.       private PersonDao personDao;     
  9.            
  10.   public PersonServiceBean(){     
  11.           System.out.println("personServiceBean.constructor() is running.");     
  12.       }     
  13.     
  14.       public PersonDao getPersonDao() {     
  15.           return personDao;     
  16.       }     
  17.        
  18.      public void setPersonDao(PersonDao personDao) {     
  19.          this.personDao = personDao;     
  20.       }     
  21.       public void save(){     
  22.           System.out.println("Name:" );     
  23.           personDao.add();     
  24.       }     
  25.       @PostConstruct    
  26.       public void init(){     
  27.          System.out.println("PersonServiceBean.init() is running.");     
  28.       }     
  29.           
  30.       @PreDestroy    
  31.       public void destory(){     
  32.           System.out.println("PersonServiceBean.destory() is running.");     
  33.       }     
  34. }  


6) SpringIOCTest.java(测试类)

[html] view plaincopy
  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.   
  7. import com.spring.service.PersonService;  
  8.   
  9. public class SpringIOCTest {  
  10.   
  11.  @BeforeClass  
  12.  public static void setUpBeforeClass() throws Exception {  
  13.  }  
  14.      @Test public void instanceSpring(){     
  15.            AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");     
  16.            PersonService personService = (PersonService)context.getBean("personService");     
  17.            personService.save();     
  18. //          context.close();     
  19.    }     
  20. }  


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

[html] view plaincopy
  1. <bean id="personService">    
  2.                 <property name="personDao">    
  3.                      <bean/>    
  4.                  </property>    
  5.  </bean>  


 2、构造器注入

以下是个例子

[html] view plaincopy
  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

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


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

控制台信息

[html] view plaincopy
  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

[html] view plaincopy
  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

[html] view plaincopy
  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.   
  8. public class PersonServiceBean implements PersonService {  
  9.   @Resource(name="personDao")  
  10.       private PersonDao personDao;     
  11.       private String name;  
  12.            
  13.   public PersonServiceBean(){     
  14.           System.out.println("personServiceBean.constructor() is running.");     
  15.       }     
  16.     
  17.    
  18.       public PersonServiceBean(PersonDao personDao, String name) {     
  19.        this.personDao = personDao;     
  20.         this.name = name;     
  21.      }     
  22.       
  23. //      public PersonDao getPersonDao() {     
  24. //          return personDao;     
  25. //      }         
  26. //     public void setPersonDao(PersonDao personDao) {     
  27. //         this.personDao = personDao;     
  28. //      }     
  29.       public void save(){     
  30.           System.out.println("Name:"+name );     
  31.           personDao.add();     
  32.       }     
  33.       @PostConstruct    
  34.       public void init(){     
  35.          System.out.println("PersonServiceBean.init() is running.");     
  36.       }     
  37.           
  38.       @PreDestroy    
  39.       public void destory(){     
  40.           System.out.println("PersonServiceBean.destory() is running.");     
  41.       }     
  42. }  


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

==================================================

http://blog.china.alibaba.com/blog/amarsoft/article/b0-i22919434.html

0 0