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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: