您好,登錄后才能下訂單哦!
本篇內容介紹了“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 {}
創建測試用例:
@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之后創建實例;
創建測試用例:
@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()
都會創建實例;
創建測試用例:
@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()
獲取后者時,得到的是同一實例,同理,其持有的前者,也是同一實例。
多次調用 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”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。