Python 官方文档:入门教程 => 点击学习
目录环境准备测试0测试1测试2测试3注:不想看具体代码的话,可以直接看每个测试的总结。 环境 ubuntu 22.04IntelliJ idea 2022.1.3jdk 17.0.3
注:不想看具体代码的话,可以直接看每个测试的总结。
修改 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
总结:
student1
)studyBook
);若把A注入B(B是singleton),playBook
)。创建测试用例:
@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;lookup-method
来指定方法名字( name
属性)和所获取的bean( bean
属性);getBook()
是Spring实现的,相当于调用了getBean()
方法来得到实例,所以每次都能获取一个新的实例(当然前提是bean必须是prototype的);getBean()
(类似懒加载);到此这篇关于Spring为singleton bean注入prototype bean的文章就介绍到这了,更多相关Spring 注入prototype bean内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Spring为singleton bean注入prototype bean
本文链接: https://www.lsjlt.com/news/153924.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0