Spring Bean作用域(Scope)

通过Spring创建一个Bean实例时,不仅可以完成Bean的实例化,还可以为Bean指定scope。例如,为了让Spring容器每次都生成一个新对象,需要把Bean的scope设置为prototype。类似的,如果你只想让Spring只生成一个实例,则你应该把scope设置为singleton。

Spring框架支持下列五种类型的scope,其中三种只有在web应用中才有效:

ScopeDescription
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)
singleton201
prototype1810

这里创建还只是及其简单的对象,若是频繁创建复杂对象,性能差距可想而知。可见频繁创建对象的性能影响之大。

转载于:https://my.oschina.net/u/877784/blog/736882

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值