通过Spring创建一个Bean实例时,不仅可以完成Bean的实例化,还可以为Bean指定scope。例如,为了让Spring容器每次都生成一个新对象,需要把Bean的scope设置为prototype。类似的,如果你只想让Spring只生成一个实例,则你应该把scope设置为singleton。
Spring框架支持下列五种类型的scope,其中三种只有在web应用中才有效:
Scope | Description |
---|---|
singleton(单例模式) | Bean在整个Spring IoC容器中只存在一个实例,Spring中默认的模式 |
prototype(原型模式) | Bean在每次通过容器调用getBean时都会生成一个新的实例 |
request | 每个Http请求都会生成一个Bean实例,该Scope只在Web应用中使用时才有效 |
session | 每个Http Session都会生成一个Bean实例,该Scope只在Web应用中使用时才有效 |
global-session | 每个全局Http Session对应一个Bean实例,该Scope只在Web应用中使用时才有效 |
两者区别的示例代码如下:
Singleton Scope
HelloWorld.java:
public class HelloWorld {
private String message;
public HelloWorld() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void showMessage() {
System.out.println("Hello World," + message);
}
}
Beans.xml(一般位于src目录下)
<?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">
<!-- Spring中默认的scope为singleton,当然你也可以显示的设置 -->
<bean class="HelloWorld">
</bean>
</beans>
Main.java
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("MainApplication.xml");
HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
helloWorld1.setMessage("Spring");
helloWorld1.showMessage();
HelloWorld helloWorld2 = ctx.getBean(HelloWorld.class);
helloWorld2.showMessage();
System.out.println("helloWorld == helloWorld2:" + (helloWorld1 == helloWorld2));
}
}
运行结果如下:
Hello World,Spring
Hello World,Spring
helloWorld == helloWorld2:true
说明helloWorld1和helloWorld2是同一个对象,即scope=singleton时,Spring容器中只会生成该Bean的一个实例。
Prototype scope
HelloWorld.java和Main.java代码同上。
Beans.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"
default-lazy-init="true"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
将bean的属性设置为prototype,则每次调用getBean方法都会生成一个新的实例
<bean class="HelloWorld" scope="prototype">
</bean>
</beans>
运行结果如下:
Hello World,Spring
Hello World,null
helloWorld == helloWorld2:false
通过上诉代码示例可以发现,如果一个Bean的scope设置为singleton,那么每次请求都会获得相同的实例。该实例的生命周期由Spring容器控制;如果一个Bean的scope设置为prototype,那么每次请求都会生成一个新的实例,这跟平时使用new来创建对象一样,一旦对象创建成功,Spring便不在维护刚刚创建完成的实例。
Java中,频繁创建对象会导致系统开销增加。使用prototype会增加创建、销毁的开销。除非有必要,否则使用默认的singleton即可。
在自己电脑上用如下代码做的测试:
Main.java
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("MainApplication.xml");
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.showMessage();
}
System.out.println("执行时间:" + (System.currentTimeMillis() - start));
}
}
HelloWorld.java
public class HelloWorld {
private String message;
public HelloWorld() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void showMessage() {
//防止打印输出对代码执行结果的影响
//System.out.println("Hello World," + message);
}
}
Beans.xml代码同上,两者执行结果分别如下
scope | 耗时(ms) |
---|---|
singleton | 201 |
prototype | 1810 |
这里创建还只是及其简单的对象,若是频繁创建复杂对象,性能差距可想而知。可见频繁创建对象的性能影响之大。