1. 程式人生 > >第五講:5.1 Spring 自動裝配

第五講:5.1 Spring 自動裝配

一,通過名字自動注入 1,複製spring40203 改名為spring40204,修改beans.xml, 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.xsd"

        default-autowire="byName"> <bean id="dog3" class="com.cruise.entity.Dog">     <property name="name" value="Jack">property> bean> <bean id="dog" class="com.cruise.entity.Dog">     <property name="name" value="Tom">property> bean> <bean id="people1" class="com.cruise.entity.People"
>     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>      bean>  beans>2,修改People類,其中Dog的屬性名決定裝配哪一個bean,例如private Dog dog3(get set toString方法要重新生成);package com.cruise.entity;public
 class People {     private int id;     private String name;     private int age;     private Dog dog;     public int getId() {        return id;     }     public void setId(int id) {        this.id = id;     }     public String getName() {        return name;     }     public void setName(String name) {        this.name = name;     }     public int getAge() {        return age;     }     public void setAge(int age) {        this.age = age;     }     public Dog getDog() {        return dog;     }     public void setDog(Dog dog) {        this.dog = dog;     }     @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";     } }3,修改T類,執行-測試package com.cruise.test;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cruise.entity.People;public class T {     private ClassPathXmlApplicationContext CPXAC=null;     @Before     public void setUp() throws Exception {        CPXAC= new ClassPathXmlApplicationContext("beans.xml");     }     @Test     public void test1() {        People people1 = (People)CPXAC.getBean("people1");        System.out.println(people1);     } }二,通過型別自動注入 1,修改beans.xml檔案,執行測試,ps:需要型別唯一,例如:class相同的bean只能有一個,所以把id為dog3的bean註釋掉,否則報錯 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.xsd"         default-autowire="byType"> <bean id="dog" class="com.cruise.entity.Dog">     <property name="name" value="Tom">property> bean> <bean id="people1" class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>      bean>  beans>2, 執行 測試(T類不用修改)     @Test     public void test1() {        People people1 = (People)CPXAC.getBean("people1");        System.out.println(people1);     }三,通過構造方法自動注入,此方法與型別方法基本一直,只是需要在People中增加構造方法。 1,修改beans.xml, 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.xsd"         default-autowire="constructor"> <bean id="dog" class="com.cruise.entity.Dog">     <property name="name" value="Tom">property> bean> <bean id="people1" class="com.cruise.entity.People">     <property name="id" value="1">property>     <property name="name" value="張三">property>     <property name="age" value="11">property>      bean>  beans>2,修改People類,新增構造方法,全參構造方法中加一個Dog即可,public People(Dog dog) {        super();        this.dog = dog;     }     public People() {        super();     }3,執行測試 @Test     public void test1() {        People people1 = (People)CPXAC.getBean("people1");        System.out.println(people1);     }