1. 程式人生 > >第五講:5.2 Spring方法注入

第五講:5.2 Spring方法注入

一,spring預設是單例模式 1,spring 預設管理的bean是單例,修改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"

> <bean id="dog" class="com.cruise.entity.Dog">     <property name="name" value="Tom">property> bean> beans>2,修改測試程式碼,執行-測試package com.cruise.test;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class
 T {     private ClassPathXmlApplicationContext CPXAC=null;     @Before     public void setUp() throws Exception {        CPXAC= new ClassPathXmlApplicationContext("beans.xml");     }     @Test     public void test1() {     System.out.println(CPXAC.getBean("dog")==CPXAC.getBean("dog"));     } }3. Dog類程式碼如下:
package com.cruise.entity;public class Dog {     private String name;     public String getName() {        return name;     }     public void setName(String name) {        this.name = name;     } }3,測試結果分析:返回為true,說明spring預設是單例模式。 二,spring修改為多例模式 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"> <bean id="dog" class="com.cruise.entity.Dog" scope="prototype">     <property name="name" value="Tom">property> bean> beans>2,測試結果分析:為false,說明每次獲取的不一樣。 三,, 1,修改beans.xml,將dog注入到People的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"> <bean id="dog1" class="com.cruise.entity.Dog" scope="prototype">     <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>     <property name="dog" ref="dog1">property> bean> beans>2,修改測試程式碼: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");        People  people2 = (People) CPXAC.getBean("people1");                 System.out.println(people1.getDog()==people2.getDog());     } }3,測試結果分析,為true, 說明是單例,說明Dog在輸入的時候已經固定死了, 四,lookup 方法注入 1. 修改People類為抽象類,修改getDog()改成抽象方法,package com.cruise.entity;public abstract 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 abstract Dog getDog();     public void setDog(Dog dog) {        this.dog = dog;     }     @Override     public String toString() {        return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";     }     public People(Dog dog) {        super();        this.dog = dog;     }     public People() {        super();     } }2. 修改beans.xml類-lookup-method標籤, <?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"> <bean id="dog1" class="com.cruise.entity.Dog" scope="prototype">     <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>     <lookup-method name="getDog" bean="dog1"/> </bean> </beans>3,T類中寫測試方法,package com.cruise.test;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cruise.entity.Dog;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");        People  people2 = (People) CPXAC.getBean("people1");         System.out.println(people1.getDog()==people2.getDog());                Dog  dog1 = (Dog) CPXAC.getBean("dog1");        Dog  dog2 = (Dog) CPXAC.getBean("dog1");        System.out.println(dog1==dog2);     } } 4,測試結果分析:兩個輸出結果都為false,說明通過修改,獲取到的bean不是同一個。