第五讲:5.2 Spring方法注入

一,spring默认是单例模式
1,spring 默认管理的bean是单例,修改beans.xml ,

xml version="1.0" encoding="UTF-8"?>
"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">

"dog" class="com.cruise.entity.Dog">
    "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"?>
"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">

"dog" class="com.cruise.entity.Dog" scope="prototype">
    "name" value="Tom">property>
bean>

beans>
2,测试结果分析:为false,说明每次获取的不一样。
三,,
1,修改beans.xml,将dog注入到People的bean中

xml version="1.0" encoding="UTF-8"?>
"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">

"dog1" class="com.cruise.entity.Dog" scope="prototype">
    "name" value="Tom">property>
bean>

"people1" class="com.cruise.entity.People">
    "id" value="1">property>
    "name" value="张三">property>
    "age" value="11">property>
    "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标签,
"1.0" encoding="UTF-8"?>
"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">

"dog1" class="com.cruise.entity.Dog" scope="prototype">
    "name" value="Tom">


"people1" class="com.cruise.entity.People">
    "id" value="1">
    "name" value="张三">
    "age" value="11">
    "getDog" bean="dog1"/>



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不是同一个。

你可能感兴趣的:(Spring基础)