Spring中的依赖注入

Spring中依赖注入的方式有两种:使用setter方法注入和使用构造器注入

第一种:set方法和构造器 -- 基本属性的注入

  set注入是一种直接方式,缺点是它假设了所有的可变属性都可以通过set方法访问到。例如有些属性在创建时设置一次,以后不再改变。

首先创建一个类,类名为PersonServiceBean.java,代码为:

  1. package cn.csdn.hr.service;  
  2.   
  3. import java.util.Date;  
  4.   
  5. publicclass PersonServiceBean {  
  6.   
  7.      //封装属性   
  8.   
  9.     private String name;  
  10.   
  11.     private String sex;  
  12.   
  13.     private Integer age;  
  14.   
  15.     private Date birth;  
  16.   
  17.     public PersonServiceBean() {  
  18.   
  19.        super();  
  20.   
  21.        // TODO Auto-generated constructor stub  
  22.   
  23.     }  
  24.   
  25.      public PersonServiceBean(String name, String sex, Integer age, Date birth) {  
  26.   
  27.        super();  
  28.   
  29.        this.name = name;  
  30.   
  31.        this.sex = sex;  
  32.   
  33.        this.age = age;  
  34.   
  35.        this.birth = birth;  
  36.   
  37.     }  
  38.   
  39.     //setter方法来依赖注入   
  40.   
  41.     publicvoid setName(String name) {  
  42.   
  43.        this.name = name;  
  44.   
  45.     }  
  46.   
  47.     publicvoid setSex(String sex) {  
  48.   
  49.        this.sex = sex;  
  50.   
  51.     }  
  52.   
  53.     publicvoid setAge(Integer age) {  
  54.   
  55.        this.age = age;  
  56.   
  57.     }  
  58.   
  59.     publicvoid setBirth(Date birth) {  
  60.   
  61.        this.birth = birth;  
  62.   
  63.     }  
  64.     @Override  
  65.   
  66.     public String toString() {  
  67.   
  68.        return"PersonServiceBean [name=" + name + ", sex=" + sex + ", age="  
  69.   
  70.               + age + ", birth=" + birth + "]";  
  71.   
  72.     }  
  73.   
  74. }  
package cn.csdn.hr.service;

import java.util.Date;

publicclass PersonServiceBean {

     //封装属性

    private String name;

    private String sex;

    private Integer age;

    private Date birth;

    public PersonServiceBean() {

       super();

       // TODO Auto-generated constructor stub

    }

     public PersonServiceBean(String name, String sex, Integer age, Date birth) {

       super();

       this.name = name;

       this.sex = sex;

       this.age = age;

       this.birth = birth;

    }

    //setter方法来依赖注入

    publicvoid setName(String name) {

       this.name = name;

    }

    publicvoid setSex(String sex) {

       this.sex = sex;

    }

    publicvoid setAge(Integer age) {

       this.age = age;

    }

    publicvoid setBirth(Date birth) {

       this.birth = birth;

    }
    @Override

    public String toString() {

       return"PersonServiceBean [name=" + name + ", sex=" + sex + ", age="

              + age + ", birth=" + birth + "]";

    }

}


在xml中用set方法注入,xml的名字为beanset.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  10.   
  11.     <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">  
  12.   
  13.        <!-- 依赖注入的方式 -->  
  14.   
  15.        <!--方式1 :通过set注入 -->  
  16.   
  17.        <property name="name">  
  18.   
  19.            <value>laowang</value>  
  20.   
  21.        </property>  
  22.   
  23.        <property name="sex">  
  24.   
  25.            <value></value>  
  26.   
  27.        </property>  
  28.   
  29.        <property name="age">  
  30.   
  31.            <value>23</value>  
  32.   
  33.        </property>  
  34.   
  35.        <!-- 出生日期 ref引用 -->  
  36.   
  37.        <property name="birth" ref="date">  
  38.   
  39.            <!-- 内部bean -->  
  40.   
  41.            <!-- <bean class="java.util.Date"></bean> -->  
  42.   
  43.        </property>  
  44.   
  45.     </bean>  
  46.   
  47.       
  48.   
  49.     <!-- 日期bean的声明  set注入 -->  
  50.   
  51.     <bean id="date" class="java.util.Date">  
  52.   
  53.        <property name="year">  
  54.   
  55.            <value>2011</value>  
  56.   
  57.        </property>  
  58.   
  59.        <property name="month">  
  60.   
  61.            <value>11</value>  
  62.   
  63.        </property>  
  64.   
  65.        <property name="date">  
  66.   
  67.            <value>1</value>  
  68.   
  69.        </property>  
  70.   
  71.     </bean>  
  72.   
  73. </beans>  
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    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">

    <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">

       <!-- 依赖注入的方式 -->

       <!--方式1 :通过set注入 -->

       <property name="name">

           <value>laowang</value>

       </property>

       <property name="sex">

           <value>女</value>

       </property>

       <property name="age">

           <value>23</value>

       </property>

       <!-- 出生日期 ref引用 -->

       <property name="birth" ref="date">

           <!-- 内部bean -->

           <!-- <bean class="java.util.Date"></bean> -->

       </property>

    </bean>

    

    <!-- 日期bean的声明  set注入 -->

    <bean id="date" class="java.util.Date">

       <property name="year">

           <value>2011</value>

       </property>

       <property name="month">

           <value>11</value>

       </property>

       <property name="date">

           <value>1</value>

       </property>

    </bean>

</beans>

 

用构造器的方法注入:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  10.   
  11.     <!-- 构造器注入必须按顺序写,如果不按顺序写,按索引,从0开始,这样顺序可以换,属性必须全写,不全都写不对 -->  
  12.   
  13.     <bean id="personServiceBean1" class="cn.csdn.hr.service.PersonServiceBean">  
  14.   
  15.        <constructor-arg index="0">  
  16.   
  17.            <value>老王</value>  
  18.   
  19.        </constructor-arg>  
  20.   
  21.        <constructor-arg>  
  22.   
  23.            <value></value>  
  24.   
  25.        </constructor-arg>  
  26.   
  27.        <constructor-arg>  
  28.   
  29.            <value>23</value>  
  30.   
  31.        </constructor-arg>  
  32.   
  33.        <constructor-arg>  
  34.   
  35.            <!-- 内部bean,只在里面使用 -->  
  36.   
  37.            <bean class="java.util.Date" />  
  38.   
  39.        </constructor-arg>  
  40.   
  41.     </bean>  
  42.   
  43.    
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    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">

    <!-- 构造器注入必须按顺序写,如果不按顺序写,按索引,从0开始,这样顺序可以换,属性必须全写,不全都写不对 -->

    <bean id="personServiceBean1" class="cn.csdn.hr.service.PersonServiceBean">

       <constructor-arg index="0">

           <value>老王</value>

       </constructor-arg>

       <constructor-arg>

           <value>女</value>

       </constructor-arg>

       <constructor-arg>

           <value>23</value>

       </constructor-arg>

       <constructor-arg>

           <!-- 内部bean,只在里面使用 -->

           <bean class="java.util.Date" />

       </constructor-arg>

    </bean>

 

    或者是:

  1.  <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">  
  2.   
  3.        <constructor-arg type="java.lang.String" value="老王" />  
  4.   
  5.        <constructor-arg type="java.lang.String" value="男" />  
  6.   
  7.        <constructor-arg type="java.lang.Integer" value="23" />  
  8.   
  9.        <constructor-arg type="java.util.Date" ref="date" />  
  10.   
  11.     </bean>  
  12.   
  13.     <bean name="date" class="java.util.Date"></bean>  
  14.   
  15. </beans>  
  16.   
  17. <STRONG><SPAN style="FONT-SIZE: 16px"> </SPAN></STRONG>  
 <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">

       <constructor-arg type="java.lang.String" value="老王" />

       <constructor-arg type="java.lang.String" value="男" />

       <constructor-arg type="java.lang.Integer" value="23" />

       <constructor-arg type="java.util.Date" ref="date" />

    </bean>

    <bean name="date" class="java.util.Date"></bean>

</beans>

 

测试的方法为:

 

  1. publicvoid test() {  
  2.   
  3.     //第一步:获取应用程序上下文对象   
  4.   
  5.     ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");  
  6.   
  7.    
  8.   
  9.     //第二步:根据应用程序上下文对象的gettBean  
  10.   
  11.     PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");  
  12.   
  13.     System.out.println(personServiceBean.toString());  
  14.   
  15.  }  
  16.   
  17. AN style="COLOR: black"> </SPAN>  
   publicvoid test() {

       //第一步:获取应用程序上下文对象

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");

    

       //第二步:根据应用程序上下文对象的gettBean

       PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");

       System.out.println(personServiceBean.toString());

    }

 

注:setter方法的注入和构造方法的注入详细解说:

    Setter方法的注入在xml中如果有没有注入的属性,则会默认为null,属性顺序的改变不会引起结果的改变。在这里,我们使用到了一个内部的bean,这个内部的bean只能供当前的属性使用,并且在setter中我们用的是ref,来引用已经创建好的内部bean

    构造方法的注入:在注入属性的时候属性的顺序在没有用index的情况下是不可以更改顺序的,更改会抛异常。如果向这样<constructor-argindex="0">每一个标签后都加一个index,顺序的改变不会抛异常,而且index是从0开始的,从以上的代码中可以看出构造器的注入方式也可以指定类型

 

第二种:setter和构造器集合的注入

普通的注入

集合的注入的类名为TeacherServiceBean.java:

集合注入分为set、map、list和Properties

代码为:

  1. package cn.csdn.hr.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import java.util.Map;  
  6.   
  7. import java.util.Properties;  
  8.   
  9. import java.util.Set;  
  10.   
  11. public class TeacherServiceBean {  
  12.   
  13.     // 集合的注入   
  14.   
  15.     private List<String> list;  
  16.   
  17.     private Set<String> set;  
  18.   
  19.     private Map<String, String> map;  
  20.   
  21.     private Properties properties;  
  22.   
  23.     public TeacherServiceBean() {  
  24.   
  25.        super();  
  26.   
  27.        // TODO Auto-generated constructor stub  
  28.   
  29.     }  
  30.   
  31.     public TeacherServiceBean(List<String> list, Set<String> set,  
  32.   
  33.            Map<String, String> map, Properties properties) {  
  34.   
  35.        super();  
  36.   
  37.        this.list = list;  
  38.   
  39.        this.set = set;  
  40.   
  41.        this.map = map;  
  42.   
  43.        this.properties = properties;  
  44.   
  45.     }  
  46.   
  47.     public void setList(List<String> list) {  
  48.   
  49.        this.list = list;  
  50.   
  51.     }  
  52.   
  53.     public void setSet(Set<String> set) {  
  54.   
  55.        this.set = set;  
  56.   
  57.     }  
  58.   
  59.     public void setMap(Map<String, String> map) {  
  60.   
  61.        this.map = map;  
  62.   
  63.     }  
  64.   
  65.     // 用户测试   
  66.   
  67.     public List<String> getList() {  
  68.   
  69.        return list;  
  70.   
  71.     }  
  72.   
  73.     public Set<String> getSet() {  
  74.   
  75.        return set;  
  76.   
  77.     }  
  78.   
  79.     public Map<String, String> getMap() {  
  80.   
  81.        return map;  
  82.   
  83.     }  
  84.   
  85.     public Properties getProperties() {  
  86.   
  87.        return properties;  
  88.   
  89.     }  
  90.   
  91.     public void setProperties(Properties properties) {  
  92.   
  93.        this.properties = properties;  
  94.   
  95.     }  
  96.   
  97.     @Override  
  98.   
  99.     public String toString() {  
  100.   
  101.        return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="  
  102.   
  103.               + map + "]";  
  104.   
  105.     }  
  106.   
  107. }  
package cn.csdn.hr.service;

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 properties;

    public TeacherServiceBean() {

       super();

       // TODO Auto-generated constructor stub

    }

    public TeacherServiceBean(List<String> list, Set<String> set,

           Map<String, String> map, Properties properties) {

       super();

       this.list = list;

       this.set = set;

       this.map = map;

       this.properties = properties;

    }

    public void setList(List<String> list) {

       this.list = list;

    }

    public void setSet(Set<String> set) {

       this.set = set;

    }

    public void setMap(Map<String, String> map) {

       this.map = map;

    }

    // 用户测试

    public List<String> getList() {

       return list;

    }

    public Set<String> getSet() {

       return set;

    }

    public Map<String, String> getMap() {

       return map;

    }

    public Properties getProperties() {

       return properties;

    }

    public void setProperties(Properties properties) {

       this.properties = properties;

    }

    @Override

    public String toString() {

       return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="

              + map + "]";

    }

}


在xml中的配置,名字为bean2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  10.   
  11.     <!-- 集合的注入 -->  
  12.   
  13.     <bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">  
  14.   
  15.        <property name="list">  
  16.   
  17.            <list>  
  18.   
  19.               <value>hp电脑1</value>  
  20.   
  21.               <value>hp电脑2</value>  
  22.   
  23.               <value>hp电脑3</value>  
  24.   
  25.               <value>hp电脑4</value>  
  26.   
  27.               <value>hp电脑5</value>  
  28.   
  29.               <value>hp电脑6</value>  
  30.   
  31.            </list>  
  32.   
  33.        </property>   
  34.   
  35.        <!-- set方式注入 -->  
  36.   
  37.        <property name="set">  
  38.   
  39.            <set>  
  40.   
  41.               <value>计算机技术1</value>  
  42.   
  43.               <value>计算机技术2</value>  
  44.   
  45.               <value>计算机技术3</value>  
  46.   
  47.               <value>计算机技术4</value>  
  48.   
  49.               <value>计算机技术5</value>  
  50.   
  51.            </set>  
  52.   
  53.        </property>  
  54.   
  55.         <!-- map集合的注入方式 -->  
  56.   
  57.        <property name="map">  
  58.   
  59.            <map>  
  60.   
  61.               <entry>  
  62.   
  63.                   <key>  
  64.   
  65.                      <value>0001</value>  
  66.   
  67.                   </key>  
  68.   
  69.                   <value>java编程与开发1</value>  
  70.   
  71.               </entry>  
  72.   
  73.               <entry>  
  74.   
  75.                   <key>  
  76.   
  77.                      <value>0002</value>  
  78.   
  79.                   </key>  
  80.   
  81.                   <value>java编程与开发2</value>  
  82.   
  83.               </entry>  
  84.   
  85.               <entry>  
  86.   
  87.                   <key>  
  88.   
  89.                      <value>0003</value>  
  90.   
  91.                   </key>  
  92.   
  93.                   <value>java编程与开发3</value>  
  94.   
  95.               </entry>  
  96.   
  97.            </map>  
  98.   
  99.        </property>  
  100.   
  101.        <!-- prop集合的配置 -->  
  102.   
  103.        <property name="properties">  
  104.   
  105.            <props>  
  106.   
  107.               <prop key="0x001">老王1</prop>  
  108.   
  109.               <prop key="0x002">老王2</prop>  
  110.   
  111.               <prop key="0x003">老王3</prop>  
  112.   
  113.            </props>  
  114.   
  115.        </property>  
  116.   
  117.     </bean>  
  118.   
  119. </beans>  
  120.   
  121.    
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    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">

    <!-- 集合的注入 -->

    <bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">

       <property name="list">

           <list>

              <value>hp电脑1</value>

              <value>hp电脑2</value>

              <value>hp电脑3</value>

              <value>hp电脑4</value>

              <value>hp电脑5</value>

              <value>hp电脑6</value>

           </list>

       </property> 

       <!-- set方式注入 -->

       <property name="set">

           <set>

              <value>计算机技术1</value>

              <value>计算机技术2</value>

              <value>计算机技术3</value>

              <value>计算机技术4</value>

              <value>计算机技术5</value>

           </set>

       </property>

        <!-- map集合的注入方式 -->

       <property name="map">

           <map>

              <entry>

                  <key>

                     <value>0001</value>

                  </key>

                  <value>java编程与开发1</value>

              </entry>

              <entry>

                  <key>

                     <value>0002</value>

                  </key>

                  <value>java编程与开发2</value>

              </entry>

              <entry>

                  <key>

                     <value>0003</value>

                  </key>

                  <value>java编程与开发3</value>

              </entry>

           </map>

       </property>

       <!-- prop集合的配置 -->

       <property name="properties">

           <props>

              <prop key="0x001">老王1</prop>

              <prop key="0x002">老王2</prop>

              <prop key="0x003">老王3</prop>

           </props>

       </property>

    </bean>

</beans>

 

获取写为构造器的注入方式:

  1. <!-- 利用构造器的注入方式 -->  
  2.   
  3.     <bean id="teacherServiceBean1" class="cn.csdn.hr.service.TeacherServiceBean">  
  4.   
  5.        <constructor-arg>  
  6.   
  7.            <list>  
  8.   
  9.               <value>hp电脑1</value>  
  10.   
  11.               <value>hp电脑2</value>  
  12.   
  13.               <value>hp电脑3</value>  
  14.   
  15.               <value>hp电脑4</value>  
  16.   
  17.               <value>hp电脑5</value>  
  18.   
  19.               <value>hp电脑6</value>  
  20.   
  21.            </list>  
  22.   
  23.        </constructor-arg>  
  24.   
  25.        <constructor-arg>  
  26.   
  27.            <set>  
  28.   
  29.               <value>计算机技术1</value>  
  30.   
  31.               <value>计算机技术2</value>  
  32.   
  33.               <value>计算机技术3</value>  
  34.   
  35.               <value>计算机技术4</value>  
  36.   
  37.               <value>计算机技术5</value>  
  38.   
  39.            </set>  
  40.   
  41.        </constructor-arg>  
  42.   
  43.        <constructor-arg>  
  44.   
  45.            <map>  
  46.   
  47.               <entry>  
  48.   
  49.                   <key>  
  50.   
  51.                      <value>0001</value>  
  52.   
  53.                   </key>  
  54.   
  55.                   <value>java编程与开发1</value>  
  56.   
  57.               </entry>  
  58.   
  59.               <entry>  
  60.   
  61.                   <key>  
  62.   
  63.                      <value>0002</value>  
  64.   
  65.                   </key>  
  66.   
  67.                   <value>java编程与开发2</value>  
  68.   
  69.               </entry>  
  70.   
  71.               <entry>  
  72.   
  73.                   <key>  
  74.   
  75.                      <value>0003</value>  
  76.   
  77.                   </key>  
  78.   
  79.                   <value>java编程与开发3</value>  
  80.   
  81.               </entry>  
  82.   
  83.            </map>  
  84.   
  85.        </constructor-arg>  
  86.   
  87.        <constructor-arg>  
  88.   
  89.            <props>  
  90.   
  91.               <prop key="0x001">老王1</prop>  
  92.   
  93.               <prop key="0x002">老王2</prop>  
  94.   
  95.               <prop key="0x003">老王3</prop>  
  96.   
  97.            </props>  
  98.   
  99.        </constructor-arg>  
  100.   
  101.     </bean>  
<!-- 利用构造器的注入方式 -->

    <bean id="teacherServiceBean1" class="cn.csdn.hr.service.TeacherServiceBean">

       <constructor-arg>

           <list>

              <value>hp电脑1</value>

              <value>hp电脑2</value>

              <value>hp电脑3</value>

              <value>hp电脑4</value>

              <value>hp电脑5</value>

              <value>hp电脑6</value>

           </list>

       </constructor-arg>

       <constructor-arg>

           <set>

              <value>计算机技术1</value>

              <value>计算机技术2</value>

              <value>计算机技术3</value>

              <value>计算机技术4</value>

              <value>计算机技术5</value>

           </set>

       </constructor-arg>

       <constructor-arg>

           <map>

              <entry>

                  <key>

                     <value>0001</value>

                  </key>

                  <value>java编程与开发1</value>

              </entry>

              <entry>

                  <key>

                     <value>0002</value>

                  </key>

                  <value>java编程与开发2</value>

              </entry>

              <entry>

                  <key>

                     <value>0003</value>

                  </key>

                  <value>java编程与开发3</value>

              </entry>

           </map>

       </constructor-arg>

       <constructor-arg>

           <props>

              <prop key="0x001">老王1</prop>

              <prop key="0x002">老王2</prop>

              <prop key="0x003">老王3</prop>

           </props>

       </constructor-arg>

    </bean>


 

测试的方法为:

  1. publicvoid teacharTest(){  
  2.   
  3.    ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");  
  4.   
  5.    TeacherServiceBean teacherServiceBean = (TeacherServiceBean) ac.getBean("teacherServiceBean1");  
  6.   
  7.    //list集合的注入方式   
  8.   
  9.    List<String> list = teacherServiceBean.getList();  
  10.   
  11.    for(String str:list){  
  12.   
  13.        System.out.println(str.toString());  
  14.   
  15.    }  
  16.   
  17.    //set集合的注入方式   
  18.   
  19.    Set<String> set = teacherServiceBean.getSet();  
  20.   
  21.    for(String str:set){  
  22.   
  23.        System.out.println(str.toString());  
  24.   
  25.    }  
  26.   
  27.    //map集合的注入方式   
  28.   
  29.    Map<String, String> map = teacherServiceBean.getMap();  
  30.   
  31.    Set<Entry<String,String>> mset = map.entrySet();  
  32.   
  33.    for(Entry str:mset){  
  34.   
  35.        System.out.println(str.toString());  
  36.   
  37.    }  
  38.   
  39.    //prop集合   
  40.   
  41.    Properties properties = teacherServiceBean.getProperties();  
  42.   
  43.    Set<Entry<Object, Object>> pset = properties.entrySet();  
  44.   
  45.    for(Entry str:pset){  
  46.   
  47.        System.out.println(str.toString());  
  48.   
  49.    }  
  50.   
  51. }  
    publicvoid teacharTest(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");

       TeacherServiceBean teacherServiceBean = (TeacherServiceBean) ac.getBean("teacherServiceBean1");

       //list集合的注入方式

       List<String> list = teacherServiceBean.getList();

       for(String str:list){

           System.out.println(str.toString());

       }

       //set集合的注入方式

       Set<String> set = teacherServiceBean.getSet();

       for(String str:set){

           System.out.println(str.toString());

       }

       //map集合的注入方式

       Map<String, String> map = teacherServiceBean.getMap();

       Set<Entry<String,String>> mset = map.entrySet();

       for(Entry str:mset){

           System.out.println(str.toString());

       }

       //prop集合

       Properties properties = teacherServiceBean.getProperties();

       Set<Entry<Object, Object>> pset = properties.entrySet();

       for(Entry str:pset){

           System.out.println(str.toString());

       }

    }


使用spring-util来完成集合的注入

在用util注入之前要先导入需要的xsd文件,

先引入http://www.springframework.org/schema/util

然后引入

http://www.springframework.org/schema/util/spring-util-2.5.xsd

最终的格式为:

<beansxmlns="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">

</beans>

 

这样就可以使用util的工具了,使用的方法如下:

  1. <bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">  
  2.   
  3.     <!-- list集合的注入 -->  
  4.   
  5.        <property name="list">  
  6.   
  7.            <util:list>  
  8.   
  9.               <value>java学习语言</value>  
  10.   
  11.            </util:list>  
  12.   
  13.        </property>  
  14.   
  15.        <!-- set集合的注入 -->  
  16.   
  17.        <property name="set">  
  18.   
  19.            <util:set>  
  20.   
  21.               <value>set集合的注入</value>  
  22.   
  23.            </util:set>  
  24.   
  25.        </property>  
  26.   
  27.        <!-- map集合的注入 -->  
  28.   
  29.        <property name="map">  
  30.   
  31.            <util:map>  
  32.   
  33.               <entry key="0x0001" value="map集合的注入">  
  34.   
  35.               </entry>  
  36.   
  37.            </util:map>  
  38.   
  39.        </property>  
  40.   
  41.        <!-- properties的注入 -->  
  42.   
  43.        <property name="properties">  
  44.   
  45.            <util:properties>  
  46.   
  47.               <prop key="0xxx">00java</prop>  
  48.   
  49.            </util:properties>  
  50.   
  51.        </property>  
  52.   
  53.     </bean>  
  54.   
  55. <STRONG> </STRONG>  
<bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">

    <!-- list集合的注入 -->

       <property name="list">

           <util:list>

              <value>java学习语言</value>

           </util:list>

       </property>

       <!-- set集合的注入 -->

       <property name="set">

           <util:set>

              <value>set集合的注入</value>

           </util:set>

       </property>

       <!-- map集合的注入 -->

       <property name="map">

           <util:map>

              <entry key="0x0001" value="map集合的注入">

              </entry>

           </util:map>

       </property>

       <!-- properties的注入 -->

       <property name="properties">

           <util:properties>

              <prop key="0xxx">00java</prop>

           </util:properties>

       </property>

    </bean>

 

空值的注入,只要在属性里写:

  1. <property name="birth">  
  2.   
  3.        <null/>  
  4.   
  5. </property>  
<property name="birth">

       <null/>

</property>

就可以了

注:我们可以看出,构造器的注入只需把set的注入的property标签改为<constructor-arg>标签就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值