spring 依赖注入方式总结详解

来源:互联网 发布:数据加密系统 编辑:程序博客网 时间:2024/06/06 00:35

Spring 能有效地组织 J2EE 应用各层的对象。不管是控制层的 Action 对象,还是业务层的 Service 对象,还是持久层的 DAO 对象,都可在 Spring 的管理下有机地协调、运行。 Spring 将各层的对象以松耦合的方式组织在一起, Action 对象无须关心 Service 对象的具体实现, Service 对象无须关心持久层对象的具体实现,各层对象的调用完全面向接口。当系统需要重构时,代码的改写量将大大减少。

上面所说的一切都得宜于 Spring 的核心机制,依赖注入。依赖注入让 bean 与 bean 之间以配置文件组织在一起,而不是以硬编码的方式耦合在一起。

理解依赖注入 :

依赖注入 (Dependency Injection) 和控制反转 (Inversion of Control) 是同一个概念。具体含义是 : 当某个角色 ( 可能是一个 Java 实例,调用者 ) 需要另一个角色 ( 另一个 Java 实例,被调用者 ) 的协助时,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在 Spring 里,创建被调用者的工作不再由调用者来完成,因此称为控制反转 ; 创建被调用者实例的工作通常由 Spring 容器来完成,然后注入调用者,因此也称为依赖注入。

案例:

package cn.csdn.spring;

import java.util.Date;

public class PersonServiceBean {
 
 private String name;
 private Integer age;
 private Date brith;

//set注入
 public void setName(String name) {
  this.name = name;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 public void setBrith(Date brith) {
  this.brith = brith;
 }
 public PersonServiceBean() {
  super();
  // TODO Auto-generated constructor stub
 }
 public PersonServiceBean(String name, Integer age, Date brith) {
  super();
  this.name = name;
  this.age = age;
  this.brith = brith;
 }
 @Override
 public String toString() {
  return "PersonServiceBean [name=" + name + ", age=" + age + ", brith="
    + brith + "]";
 }
 
 

}

applicationContext.xml
第一种方法:

<bean id="personServiceBean" class="cn.csdn.spring.PersonServiceBean">
      <!-- 依赖注入的方式 -->
      <property name="name"><value>zhangxiaocong</value></property>
      <property name="age"><value>33</value></property>
      <property name="brith" ref="date">
     
     
     
      </property>
      <!-- 出生日期 -->
    </bean>
    <bean id="date" class="java.util.Date">
     <property name="year"><value>97</value></property>
     <property name="month"><value>1</value></property>
     <property name="date"><value>1</value></property>
   </bean>

第二种方法:

 <bean id="personServiceBean1" class="cn.csdn.spring.PersonServiceBean">
     <!-- 构造器注入:全部都要对象,不能少属性的构造 -->
     <constructor-arg index="0">
      <value>dfdfdfd</value>
     
     
     </constructor-arg>
   
     <constructor-arg index="1">
      <value>28</value>
     
     
     </constructor-arg>
   
     <constructor-arg index="2">
     <bean  class="java.util.Date"></bean>
     
     
     </constructor-arg>
   
    </bean>
    <bean id="personServiceBean" class="cn.csdn.spring.PersonServiceBean">
     <!-- 构造器注入:按照类型进行匹配-->
      
     <constructor-arg type="java.lang.String" value="小弟"/>
     <constructor-arg type="java.lang.Integer" value="21"/>
     <constructor-arg type="java.util.Date" ref="date"/>
     
    </bean>
   
     <bean id="date" class="java.util.Date">
     <property name="year"><value>97</value></property>
     <property name="month"><value>1</value></property>
     <property name="date"><value>1</value></property>
     
    </bean>
   测试:
 public void test() {

//获取应用程序上下文
  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

第二步:根据应用程序上下文对象的getBean("Id名称")获取bean的实例对象
  PersonServiceBean p=  (PersonServiceBean) context.getBean("personServiceBean");
  System.out.println(p.toString());
 }  

集合的依赖注入

package cn.csdn.spring;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TeacherServiceBean {
 private List<String> list;
 private Set<String> set;
 private Map<String,String> map;
 
 private Properties p;
 public Properties getP() {
  return p;
 }
 public void setP(Properties p) {
  this.p = p;
 }
 public List<String> getList() {
  return list;
 }
 public void setList(List<String> list) {
  this.list = list;
 }
 public Set<String> getSet() {
  return set;
 }
 public void setSet(Set<String> set) {
  this.set = set;
 }
 public Map<String, String> getMap() {
  return map;
 }
 public void setMap(Map<String, String> map) {
  this.map = map;
 }
 
 public TeacherServiceBean(List<String> list, Set<String> set,
   Map<String, String> map, Properties p) {
  super();
  this.list = list;
  this.set = set;
  this.map = map;
  this.p = p;
 }
 public TeacherServiceBean() {
  super();
  // TODO Auto-generated constructor stub
 }
 @Override
 public String toString() {
  return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="
    + map + "]";
 }
 

}

第一种方法
<bean id="teacherServiceBean1" class="cn.csdn.spring.TeacherServiceBean">
       <property name="list">
        <list>
         <value>eedfsds</value>
         <value>dfdd</value>
         <value>ee</value>
         <value>eeeed</value>
         <value>ddfvfvfd</value>
         <value>dcdd</value>
         <value>dd</value>
         <value>cdfgdv</value>
        
        
        </list>
       
       
       
       </property>
       <property name="set">
        <set>
         <value>eedfsds</value>
         <value>dfdd</value>
         <value>ee</value>
         <value>eeeed</value>
         <value>ddfvfvfd</value>
         <value>dcdd</value>
         <value>dd</value>
         <value>cdfgdv</value>
        
        
        </set>
       
       
       
       </property>
       <property name="map">
        <map>
        <entry>
         <key><value>ffffff</value></key>
         <value>fffffffffff</value>
        
        </entry>
        
        
        </map>
       
       
       
       </property>
       
       <property name="p">
       
       <props>
        <prop key="00001"> sddsdsd</prop>
        <prop key="00001"> sddsd</prop>
        <prop key="00001"> sd</prop>
        <prop key="00001"> sdds</prop>
       </props>
       
       
       </property>
   
   
   
   
    </bean>
   
   
   
   
   
   
   
    <bean id="teacherServiceBean" class="cn.csdn.spring.TeacherServiceBean">
       <constructor-arg>
        <list>
         <value>eedfsds</value>
         <value>dfdd</value>
         <value>ee</value>
         <value>eeeed</value>
         <value>ddfvfvfd</value>
         <value>dcdd</value>
         <value>dd</value>
         <value>cdfgdv</value>
        
        
        </list>
       
       
       
       </constructor-arg>
       <constructor-arg>
        <set>
         <value>eedfsds</value>
         <value>dfdd</value>
         <value>ee</value>
         <value>eeeed</value>
         <value>ddfvfvfd</value>
         <value>dcdd</value>
         <value>dd</value>
         <value>cdfgdv</value>
        
        
        </set>
       
       
       
       </constructor-arg>
      <constructor-arg>
        <map>
        <entry>
         <key><value>ffffff</value></key>
         <value>fffffffffff</value>
        
        </entry>
        
        
        </map>
       
       
       
       </constructor-arg>
       
       <constructor-arg>
       
       <props>
        <prop key="00001"> sddsdsd</prop>
        <prop key="00001"> sddsd</prop>
        <prop key="00001"> sd</prop>
        <prop key="00001"> sdds</prop>
       </props>
       
       
       </constructor-arg>
   
   
   
   
    </bean>
 第二种方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-2.5.xsd
          
           ">
        
    <bean id="teacherServiceBean" class="cn.csdn.spring.TeacherServiceBean">
       <property name="list">
        <util:list>
         <value>java书</value>
       
        </util:list>
       </property>
       
       <property name="set">
       
        <util:set>
        
         <value>c++</value>
        
        
        </util:set>
       
       </property>
       
       <property name="map">
       
        <util:map>
         <entry key="0000s00001" value="java编程开发基础"></entry>
        </util:map>
       </property>
   
     <property name="p">
      <util:properties>
      
      
      </util:properties>
     </property>
   
   
   
    </bean>
    
      
</beans>
 测试:

public void test2() {
  ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext3.xml");
  TeacherServiceBean p=  (TeacherServiceBean) context.getBean("teacherServiceBean");
  
  
  List<String> list=p.getList();
  for(String str:list){
   System.out.println("List的集合的反馈信息"+str);
   
  }
  
  Set<String> set=p.getSet();
  
  for(String str:set){
   System.out.println("Set集合返回的值"+str);
  }
  
  
  
  
  Map<String,String> map=p.getMap();
  
  Set<Entry<String,String>> msp=map.entrySet();
  
  for(Entry str:msp){
   System.out.println("Map集合返回的值"+str.getKey()+"---"+str.getValue());
   
  }
  
  
  
  
  
  Properties prop=  p.getP();
  Set<Entry<Object,Object>> ppp=prop.entrySet();
  for(Entry entry:ppp){
   System.out.println("Properties集合返回的值"+entry.getKey()+"________"+entry.getValue());
   
  }
  
  
 }

原创粉丝点击