1. 程式人生 > >第四講:4.2 Spring注入引數

第四講:4.2 Spring注入引數

一,Spring注入引數-bean 1,在com.cruise.entity包下新建Dao類,屬性name,package com.cruise.entity;public class Dog {     private String name;     public String getName() {        return name;     }     public void setName(String name) {        this.name = name;     } }2,修改People屬性,增加Dog屬性,重新生成get 和set 方法,修改toString()方法,

  @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";     }3,在beans.xml中定義id為dog1,注入屬性值。定義id為people2,注入屬性值, <bean id="dog1" class="com.cruise.entity.Dog">     <property name="name" value="Jack">property> bean> <bean id="people2"
 class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>     <property name="dog" ref="dog1">property> bean>4,Test類中新增測試方法test2(),執行-測試 @Test     public
 void test2() {        People people2 = (People)CPXAC.getBean("people2");        System.out.println(people2);     }二,Spring注入引數-內部bean1,在beans.xml中新增程式碼,定義people3,注入屬性值,新增內部bean, <bean id="people3" class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>     <property name="dog" >        <bean class="com.cruise.entity.Dog">            <property name="name" value="Tom">property>        bean>     property> bean>2,在Test中新增測試方法test3(),執行-測試 @Test     public void test3() {        People people3 = (People)CPXAC.getBean("people3");        System.out.println(people3);     }三,Spring注入引數-null標籤bean1,在beans.xml中新增程式碼,定義people4,注入屬性值null標籤,新增內部bean, <bean id="people4" class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>     <property name="dog" >        <null>null>     property> bean>3,修改People類中的toString()方法,否則會報空指標異常 @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + "]";     }2,在Test中新增測試方法test4(),執行-測試 @Test     public void test4() {        People people3 = (People)CPXAC.getBean("people4");        System.out.println(people3);     }四,Spring注入引數-級聯屬性引數1,在beans.xml中新增程式碼,定義people5,注入屬性值,新增內部bean, <bean id="people5" class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>     <property name="dog.name" value="Jack2">property> bean>2,修改People類中的Dog屬性,修改toString,private Dog dog=new Dog(); @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";     }3,在Test中新增測試方法test5(),執行-測試,測試之後還原People類dog屬性寫法,註釋掉beans.xml中的people5.以防干擾。 @Test     public void test5() {        People people5 = (People)CPXAC.getBean("people5");        System.out.println(people5);     }五,Spring注入引數-集合List屬性引數1,修改People類中屬性,增加一個集合屬性。重新生成toString()方法,get set 方法,private List<String> hobbies = new ArrayList<String>(); @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies + "]";     }2,在beans.xml中新增程式碼,定義people6,注入屬性值,新增內部bean,將id為people5的bean註釋掉, <bean id="dog1" class="com.cruise.entity.Dog">     <property name="name" value="Jack"></property> </bean>  …… <bean id="people6" class="com.cruise.entity.People">     <property name="id" value="1"></property>     <property name="name" value="張三"></property>     <property name="age" value="11"></property>     <property name="dog" ref="dog1"></property>     <property name="hoggies">        <list>            <value>唱歌</value>            <value>跳舞</value>        </list>     </property> </bean>3,在Test中新增測試方法test6(),執行-測試, @Test     public void test6() {        People people5 = (People)CPXAC.getBean("people6");        System.out.println(people5);     }六,Spring注入引數-集合Set屬性引數1,修改People類中屬性,增加一個Set集合屬性。重新生成toString()方法,get set 方法,private Set<String> loves = new HashSet<String>(); @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies               + ", loves=" + loves + "]";     }2,在beans.xml中新增程式碼,定義people7,注入屬性值,新增內部bean, <bean id="dog1" class="com.cruise.entity.Dog">     <property name="name" value="Jack"></property> </bean>  …… <bean id="people7" class="com.cruise.entity.People">     <property name="id" value="1"></property>     <property name="name" value="張三"></property>     <property name="age" value="11"></property>     <property name="dog" ref="dog1"></property>     <property name="hoggies">        <list>            <value>唱歌</value>            <value>跳舞</value>        </list>     </property>   <property name="loves">        <set>            <value>唱歌2</value>            <value>跳舞2</value>        </set>     </property> </bean>3,在Test中新增測試方法test7(),執行-測試, @Test     public void test7() {        People people7 = (People)CPXAC.getBean("people7");        System.out.println(people7);     }七,Spring注入引數-集合Map屬性引數1,修改People類中屬性,增加一個Map集合屬性。重新生成toString()方法,get set 方法,private Map<String,String> works = new HashMap<String,String>(); @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies               + ", loves=" + loves + ", works=" + works + "]";     }2,在beans.xml中新增程式碼,定義people8,注入屬性值,新增內部bean, <bean id="dog1" class="com.cruise.entity.Dog">     <property name="name" value="Jack"></property> </bean>  …… <bean id="people8" class="com.cruise.entity.People">     <property name="id" value="1"></property>     <property name="name" value="張三"></property>     <property name="age" value="11"></property>     <property name="dog" ref="dog1"></property>     <property name="hobbies">        <list>            <value>唱歌</value>            <value>跳舞</value>        </list>     </property>     <property name="loves">        <set>            <value>唱歌2</value>            <value>跳舞2</value>        </set>     </property>     <property name="works">        <map>            <entry>               <key><value>上午</value></key>                <value>寫作業</value>            </entry>            <entry>               <key><value>下午</value></key>                <value>出去玩</value>            </entry>        </map>     </property> </bean>3,在Test中新增測試方法test8(),執行-測試, @Test     public void test8() {        People people7 = (People)CPXAC.getBean("people8");        System.out.println(people7);     }八,Spring注入引數-集合Properties屬性引數1,修改People類中屬性,增加一個集合屬性。重新生成toString()方法,get set 方法,private Properties addresses = new Properties(); @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + ", hobbies=" + hobbies               + ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]";     }2,在beans.xml中新增程式碼,定義people9,注入屬性值,新增內部bean, <bean id="dog1" class="com.cruise.entity.Dog">     <property name="name" value="Jack"></property> </bean>  …… <bean id="people9" class="com.cruise.entity.People">     <property name="id" value="1"></property>     <property name="name" value="張三"></property>     <property name="age" value="11"></property>     <property name="dog" ref="dog1"></property>     <property name="hobbies">        <list>            <value>唱歌</value>            <value>跳舞</value>        </list>     </property>     <property name="loves">        <set>            <value>唱歌2</value>            <value>跳舞2</value>        </set>     </property>     <property name="works">        <map>            <entry>               <key><value>上午</value></key>                <value>寫作業</value>            </entry>            <entry>               <key><value>下午</value></key>                <value>出去玩</value>            </entry>        </map>     </property>     <property name="addresses">        <props>            <prop key="地址1">北京</prop>            <prop key="地址2">江蘇</prop>        </props>     </property> </bean>3,在Test中新增測試方法test9(),執行-測試,  @Test     public void test9() {        People people7 = (People)CPXAC.getBean("people9");        System.out.println(people7);     }