Spring為singleton?bean注入prototype?bean
注:不想看具體代碼的話,可以直接看每個(gè)測(cè)試的總結(jié)。
環(huán)境
- Ubuntu 22.04
- IntelliJ IDEA 2022.1.3
- JDK 17.0.3
- Spring 5.3.21
準(zhǔn)備
創(chuàng)建Maven項(xiàng)目 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> ......
創(chuàng)建如下POJO:
Book
:Book接口;PlayBook
:Book實(shí)現(xiàn)類;StudyBook
:Book實(shí)現(xiàn)類;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
目錄下創(chuàng)建 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
目錄下創(chuàng)建測(cè)試:
public class Test0707 {}
測(cè)試0
創(chuàng)建測(cè)試用例:
@Test public void test0() { var ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); }
運(yùn)行測(cè)試,如下:
Student1 constructor
PlayBook constructor
總結(jié):
- singleton的bean會(huì)在Spring初始化時(shí)創(chuàng)建實(shí)例(如本例中的
student1
) - ;prototype的bean不會(huì)在Spring初始化時(shí)創(chuàng)建實(shí)例(如本例中的
studyBook
);若把A注入B(B是singleton), - 則A在Spring初始化時(shí)隨著B一起創(chuàng)建實(shí)例(如本例中的
playBook
)。 - 接上條,若把A注入B(B是singleton),如果A是singleton,則A在B之前創(chuàng)建實(shí)例。如果A是prototype,則A在B之后創(chuàng)建實(shí)例;
測(cè)試1
創(chuàng)建測(cè)試用例:
@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); }
運(yùn)行測(cè)試,如下:
Student1 constructor
PlayBook constructor
before getBean student1 playBook
true
PlayBook constructor
PlayBook constructor
false
總結(jié):
singleton的bean,只在Spring初始化時(shí)創(chuàng)建實(shí)例, getBean()
不會(huì)創(chuàng)建實(shí)例;prototype的bean,不在Spring初始化時(shí)創(chuàng)建實(shí)例(注入例外),每次 getBean()
都會(huì)創(chuàng)建實(shí)例;
測(cè)試2
創(chuàng)建測(cè)試用例:
@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()); }
運(yùn)行測(cè)試,如下:
Student1 constructor
PlayBook constructor
before getBean student1
true
true
總結(jié):
把prototype的bean注入到singleton,多次調(diào)用 getBean()
獲取后者時(shí),得到的是同一實(shí)例,同理,其持有的前者,也是同一實(shí)例。
測(cè)試3
多次調(diào)用 getBean()
方法獲取singleton bean時(shí),對(duì)于所注入的prototype的bean,如果希望每次都獲取一個(gè)新的bean實(shí)例,可以使用 lookup-method
來(lái)配置。
例如:
<lookup-method name="getBook" bean="playBook"/>
完整例子如下:
創(chuàng)建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
文件中注冊(cè)bean:
<bean id="student2" class="pojo.Student2"> <property name="name" value="Jerry"/> <!-- <property name="book" ref="playBook"/>--> <lookup-method name="getBook" bean="playBook"/> </bean>
創(chuàng)建測(cè)試用例:
@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()); }
運(yùn)行測(cè)試,如下:
......
Student2 constructor
before getBean student2
true
PlayBook constructor
PlayBook constructor
false
總結(jié):
Student2
是抽象類,getBook()
是抽象方法;Student2
并不持有Book,只需使用getBook()
方法來(lái)得到Book;- 在Spring配置中使用
lookup-method
來(lái)指定方法名字(name
屬性)和所獲取的bean(bean
屬性);getBook()
是Spring實(shí)現(xiàn)的,相當(dāng)于調(diào)用了 getBean()
方法來(lái)得到實(shí)例,所以每次都能獲取一個(gè)新的實(shí)例(當(dāng)然前提是bean必須是prototype的);- singleton bean在Spring初始化時(shí)創(chuàng)建實(shí)例,lookup的bean不會(huì)隨著一起創(chuàng)建實(shí)例,只有在顯式調(diào)用lookup方法時(shí)才會(huì)
getBean()
(類似懶加載);
到此這篇關(guān)于Spring為singleton bean注入prototype bean的文章就介紹到這了,更多相關(guān)Spring 注入prototype bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)FTP服務(wù)器功能實(shí)例代碼
FTP(File Transfer Protocol 文件傳輸協(xié)議)是Internet 上用來(lái)傳送文件的協(xié)議,本文給大家分享Java實(shí)現(xiàn)FTP服務(wù)器功能實(shí)例代碼,對(duì)java實(shí)現(xiàn)ftp服務(wù)器相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12Java中基于maven實(shí)現(xiàn)zxing二維碼功能
這篇文章主要介紹了Java中基于maven實(shí)現(xiàn)zxing二維碼功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02@Autowired自動(dòng)裝配,@Bean注入@Primary,@Qualifier優(yōu)先級(jí)講解
這篇文章主要介紹了@Autowired自動(dòng)裝配,@Bean注入@Primary,@Qualifier優(yōu)先級(jí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring?Boot項(xiàng)目Jar包加密實(shí)戰(zhàn)教程
本文詳細(xì)介紹了如何在Spring?Boot項(xiàng)目中實(shí)現(xiàn)Jar包加密,我們首先了解了Jar包加密的基本概念和作用,然后學(xué)習(xí)了如何使用Spring?Boot的Jar工具和第三方庫(kù)來(lái)實(shí)現(xiàn)Jar包的加密和解密,感興趣的朋友一起看看吧2024-02-022020最新 idea下載、安裝與創(chuàng)建項(xiàng)目測(cè)試的教程圖解
這篇文章主要介紹了2020最新 idea下載、安裝與創(chuàng)建項(xiàng)目測(cè)試的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08重試框架Guava-Retry和spring-Retry的使用示例
spring-retry 和 guava-retry 工具都是線程安全的重試,能夠支持并發(fā)業(yè)務(wù)場(chǎng)景的重試邏輯正確性,本文主要介紹了重試框架Guava-Retry和spring-Retry的使用示例,感興趣的可以一下2023-09-09Java實(shí)現(xiàn)調(diào)用對(duì)方http接口得到返回?cái)?shù)據(jù)
這篇文章主要介紹了Java實(shí)現(xiàn)調(diào)用對(duì)方http接口得到返回?cái)?shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09