Spring为singleton bean注入prototype bean

 更新时间:2022年07月08日 15:41:53   作者:蓝黑2020  
这篇文章主要介绍了Spring为singleton bean注入prototype bean,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

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

环境

  • Ubuntu 22.04
  • IntelliJ IDEA 2022.1.3
  • JDK 17.0.3
  • Spring 5.3.21

准备

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

  • Book :Book接口;
  • PlayBook :Book实现类;
  • StudyBook :Book实现类;
  • Student1 :Student1持有Book;
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

总结:

  • singleton的bean会在Spring初始化时创建实例(如本例中的 student1
  • ;prototype的bean不会在Spring初始化时创建实例(如本例中的 studyBook );若把A注入B(B是singleton),
  • 则A在Spring初始化时随着B一起创建实例(如本例中的 playBook )。
  • 接上条,若把A注入B(B是singleton),如果A是singleton,则A在B之前创建实例。如果A是prototype,则A在B之后创建实例;

测试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

总结:

  • Student2 是抽象类, getBook() 是抽象方法;
  • Student2 并不持有Book,只需使用 getBook() 方法来得到Book;
  • 在Spring配置中使用 lookup-method 来指定方法名字( name 属性)和所获取的bean( bean 属性);getBook() 是Spring实现的,相当于调用了
  • getBean() 方法来得到实例,所以每次都能获取一个新的实例(当然前提是bean必须是prototype的);
  • singleton bean在Spring初始化时创建实例,lookup的bean不会随着一起创建实例,只有在显式调用lookup方法时才会 getBean() (类似懒加载);

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

相关文章

  • Java实现FTP服务器功能实例代码

    Java实现FTP服务器功能实例代码

    FTP(File Transfer Protocol 文件传输协议)是Internet 上用来传送文件的协议,本文给大家分享Java实现FTP服务器功能实例代码,对java实现ftp服务器相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • 详解Java拦截器以及自定义注解的使用

    详解Java拦截器以及自定义注解的使用

    这篇文章主要为大家介绍了Java拦截器以及自定义注解的使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2021-12-12
  • Java中基于maven实现zxing二维码功能

    Java中基于maven实现zxing二维码功能

    这篇文章主要介绍了Java中基于maven实现zxing二维码功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • @Autowired自动装配,@Bean注入@Primary,@Qualifier优先级讲解

    @Autowired自动装配,@Bean注入@Primary,@Qualifier优先级讲解

    这篇文章主要介绍了@Autowired自动装配,@Bean注入@Primary,@Qualifier优先级,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringMVC中的HandlerMapping详解

    SpringMVC中的HandlerMapping详解

    这篇文章主要介绍了SpringMVC中的HandlerMapping详解,HandlerMapping是请求映射处理器,也就是通过请求的url找到对应的逻辑处理单元(Controller),注意这里只是建立请求与Controller的映射关系,最终的处理是通过HandlerAdapt来进行处理的,需要的朋友可以参考下
    2023-09-09
  • Spring Boot项目Jar包加密实战教程

    Spring Boot项目Jar包加密实战教程

    本文详细介绍了如何在Spring Boot项目中实现Jar包加密,我们首先了解了Jar包加密的基本概念和作用,然后学习了如何使用Spring Boot的Jar工具和第三方库来实现Jar包的加密和解密,感兴趣的朋友一起看看吧
    2024-02-02
  • 2020最新 idea下载、安装与创建项目测试的教程图解

    2020最新 idea下载、安装与创建项目测试的教程图解

    这篇文章主要介绍了2020最新 idea下载、安装与创建项目测试的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 重试框架Guava-Retry和spring-Retry的使用示例

    重试框架Guava-Retry和spring-Retry的使用示例

    spring-retry 和 guava-retry 工具都是线程安全的重试,能够支持并发业务场景的重试逻辑正确性,本文主要介绍了重试框架Guava-Retry和spring-Retry的使用示例,感兴趣的可以一下
    2023-09-09
  • Java实现调用对方http接口得到返回数据

    Java实现调用对方http接口得到返回数据

    这篇文章主要介绍了Java实现调用对方http接口得到返回数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringBoot详细讲解yaml配置文件的用法

    SpringBoot详细讲解yaml配置文件的用法

    这篇文章主要介绍了SpringBoot中的yaml配置文件问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06

最新评论