Spring - Bean 的作用域

22 篇文章 0 订阅
14 篇文章 0 订阅

当定义一个Bean的时候,你可以选择声明这个Bean的scope,不知道怎么翻译这个词。
When defining a you have the option of declaring a scope for that bean.
比如,你可以强制每次实例化一个新的对象,或者每次都返回同一个对象,这个可以说就是一个单例。

Scope

Spring框架支持一下五种scopes,其中三种只有在使用Web感知的 ApplicationContext的时候才可以使用。

NoScopeDescription
1singletonSpring Ioc 容器默认使用该scope,即单例模式
2prototype一个Bean定义,可以有多个实例对象
3request在Http request中的定义,仅在Web感知的ApplicationContext中才有效
4session在Http Session中的定义,仅在Web感知的ApplicationContext中才有效
5global-session在全局的Http Session中的定义,仅在Web感知的ApplicationContext中才有效

注:后面三个限定了使用范围,放在后面讨论

Singleton Scope

如果一个Bean被定义为Singleton,那么IoC容器回根据bean的定义每次都只创建一个对象。这个单例的bean实例存储在单例Beans的cache中,后续请求相同名称的bean,都只返回相同的bean,不会在重新创建。

当你在Bean中不声明Scope,那么默认该Bean为单例(Singleton)。如果你需要单例的Bean对象,你可以像下面这样配置Bean,也可以不声明scope。

<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
   <!-- collaborators and configuration for this bean go here -->
</bean>
Example

HelloWorld.java

package com.soygrow;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }
}

MainApp.java

package com.soygrow;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        helloWorld.getMessage();
        helloWorld.setMessage("singleton test.");

        HelloWorld helloWorld1 = (HelloWorld) context.getBean("helloWorld");
        helloWorld1.getMessage();

        ((ClassPathXmlApplicationContext) context).close();
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloWorld" class="com.soygrow.HelloWorld" scope="singleton">
        <property name="message" value="Hello Soygrow!"/>
    </bean>
</beans>

在beans.xml中scope不定义也是可以的,打开可以试一下。如果一切正常得到的结果应该是下面的:

Your Message : Hello Soygrow!
Your Message : singleton test.

Prototype Scope

如果bean的scope被设置成了prototype,Spring IoC容器每次都会创建新的Bean的实例。all state-full 的Beans使用prototype,stateless的bean使用singleton。

如果想每次都创建一个新的Bean,那么需要像下面这样配置Bean

<!-- A bean definition with prototype scope -->
<bean id = "..." class = "..." scope = "prototype">
   <!-- collaborators and configuration for this bean go here -->
</bean>

Example

其他文件的代码没有变化,只有xml文件中scope参数改变。
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloWorld" class="com.soygrow.HelloWorld" scope="prototype">
        <property name="message" value="Hello Soygrow!"/>
    </bean>
</beans>

如果一切正常,结果应该是下面这个:

Your Message : Hello Soygrow!
Your Message : Hello Soygrow!

遇到的问题

这小节主要讲了两个scope,一个单例,一个非单例。比较简单,目前没有遇到问题。

Spring教程专栏地址:http://blog.csdn.net/column/details/19452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值