首頁 > 軟體

Spring為singleton bean注入prototype bean

2022-07-08 18:06:24

注:不想看具體程式碼的話,可以直接看每個測試的總結。

環境

  • 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內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com