注:不想看具体代码的话,可以直接看每个测试的总结。
环境
- 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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!