亲宝软件园·资讯

展开

Spring 注入prototype bean

蓝黑2020 人气:0

注:不想看具体代码的话,可以直接看每个测试的总结。

环境

准备

创建Maven项目 test0707

修改 pom.xml 文件,添加依赖:

        ......
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.21</version>
        </dependency>
        ......

创建如下POJO:

package pojo;
public interface Book {
    public void show();
}
package pojo;
public class PlayBook implements Book{
    public PlayBook() {
        System.out.println("PlayBook constructor");
    }
    @Override
    public void show() {
        System.out.println("Play book!");
    }
}
package pojo;

public class StudyBook implements Book{
    public StudyBook() {
        System.out.println("StudyBook constructor");
    }
    @Override
    public void show() {
        System.out.println("Study book!");
    }
}
package pojo;

public class Student1 {
    private String name;
    private Book book;
    public void setName(String name) {
        this.name = name;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public Book getBook() {
        return book;
    }
    public Student1() {
        System.out.println("Student1 constructor");
    }
    public void readBook() {
        System.out.println("I am " + name);
        book.show();
    }
}

src/main/resources 目录下创建 applicationContext.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="playBook" class="pojo.PlayBook" scope="prototype"/>

    <bean id="studyBook" class="pojo.StudyBook" scope="prototype"/>

    <bean id="student1" class="pojo.Student1">
        <property name="name" value="Jerry"/>
        <property name="book" ref="playBook"/>
    </bean>
</beans>

src/test/java 目录下创建测试:

public class Test0707 {}

测试0

创建测试用例:

    @Test
    public void test0() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

运行测试,如下:

Student1 constructor
PlayBook constructor

总结:

测试1

创建测试用例:

    @Test
    public void test1() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("before getBean student1 playBook");
        var student1 = ctx.getBean("student1", Student1.class);
        var student2 = ctx.getBean("student1", Student1.class);
        System.out.println(student1 == student2);

        var book1 = ctx.getBean("playBook", PlayBook.class);
        var book2 = ctx.getBean("playBook", PlayBook.class);
        System.out.println(book1 == book2);
    }

运行测试,如下:

Student1 constructor
PlayBook constructor
before getBean student1 playBook
true
PlayBook constructor
PlayBook constructor
false

总结:

singleton的bean,只在Spring初始化时创建实例, getBean() 不会创建实例;prototype的bean,不在Spring初始化时创建实例(注入例外),每次 getBean() 都会创建实例;

测试2

创建测试用例:

    @Test
    public void test2() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("before getBean student1");
        var student1 = ctx.getBean("student1", Student1.class);
        var student2 = ctx.getBean("student1", Student1.class);
        System.out.println(student1 == student2);
        System.out.println(student1.getBook() == student2.getBook());
    }

运行测试,如下:

Student1 constructor
PlayBook constructor
before getBean student1
true
true

总结:

把prototype的bean注入到singleton,多次调用 getBean() 获取后者时,得到的是同一实例,同理,其持有的前者,也是同一实例。

测试3

多次调用 getBean() 方法获取singleton bean时,对于所注入的prototype的bean,如果希望每次都获取一个新的bean实例,可以使用 lookup-method 来配置。

例如:

        <lookup-method name="getBook" bean="playBook"/>

完整例子如下:

创建POJO Student2

package pojo;
public abstract class Student2 {
    private String name;
//    private Book book;
    public void setName(String name) {
        this.name = name;
    }
//    public void setBook(Book book) {
//        this.book = book;
//    }
//
//    public Book getBook() {
//        return book;
//    }
    public abstract Book getBook();
    public Student2() {
        System.out.println("Student2 constructor");
    }
    public void readBook() {
        System.out.println("I am " + name);
//        book.show();
        getBook().show();
    }
}

applicationContext.xml 文件中注册bean:

    <bean id="student2" class="pojo.Student2">
        <property name="name" value="Jerry"/>
<!--        <property name="book" ref="playBook"/>-->
        <lookup-method name="getBook" bean="playBook"/>
    </bean>

创建测试用例:

    @Test
    public void test3() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("before getBean student2");
        var student1 = ctx.getBean("student2", Student2.class);
        var student2 = ctx.getBean("student2", Student2.class);
        System.out.println(student1 == student2);
        System.out.println(student1.getBook() == student2.getBook());
    }

运行测试,如下:

......
Student2 constructor
before getBean student2
true
PlayBook constructor
PlayBook constructor
false

总结:

加载全部内容

相关教程
猜你喜欢
用户评论