java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring 注入prototype bean

Spring为singleton bean注入prototype bean

作者:蓝黑2020

这篇文章主要介绍了Spring为singleton bean注入prototype bean,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

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

环境

准备

创建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

总结:

到此这篇关于Spring为singleton bean注入prototype bean的文章就介绍到这了,更多相关Spring 注入prototype bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文