zoukankan      html  css  js  c++  java
  • Spring Bean的作用域(转)

    Spring Bean的作用域

    .singleton  [单例] eg:<bean id="personService" class="com.yinger.service.impl.PersonServiceBean" scope="singleton"></bean>

    在每个Spring IoC容器中一个bean定义只有一个对象实例。

    请注意Spring的singleton bean概念与“四人帮”(GoF)模式一书中定义的Singleton模式是完全不同的。

    经典的GoF Singleton模式中所谓的对象范围是指在每一个ClassLoader指定class创建的实例有且仅有一个

    把Spring的singleton作用域描述成一个container对应一个bean实例最为贴切。亦即,假如在单个Spring容器内定义了某个指定class的bean,

    那么Spring容器将会创建一个且仅有一个由该bean定义指定的类实例。

    org.springframework.beans.factory.config.ConfigurableBeanFactory

    /**
     * Configuration interface to be implemented by most bean factories. Provides
     * facilities to configure a bean factory, in addition to the bean factory
     * client methods in the {@link org.springframework.beans.factory.BeanFactory}
     * interface.
     *
     * <p>This bean factory interface is not meant to be used in normal application
     * code: Stick to {@link org.springframework.beans.factory.BeanFactory} or
     * {@link org.springframework.beans.factory.ListableBeanFactory} for typical
     * needs. This extended interface is just meant to allow for framework-internal
     * plug'n'play and for special access to bean factory configuration methods.
     *
     * @author Juergen Hoeller
     * @since 03.11.2003
     * @see org.springframework.beans.factory.BeanFactory
     * @see org.springframework.beans.factory.ListableBeanFactory
     * @see ConfigurableListableBeanFactory
     */
    public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {
    
        /**
         * Scope identifier for the standard singleton scope: "singleton".
         * Custom scopes can be added via {@code registerScope}.
         * @see #registerScope
         */
        String SCOPE_SINGLETON = "singleton";
    
        /**
         * Scope identifier for the standard prototype scope: "prototype".
         * Custom scopes can be added via {@code registerScope}.
         * @see #registerScope
         */
        String SCOPE_PROTOTYPE = "prototype";

    默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。

    如:<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>

    如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:

    <beans default-lazy-init="true“ ...>

    .prototype [原型]

    每次从容器获取bean都是新的对象。

    对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是

    装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法。

    但对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,

    都是客户端代码的职责。(让Spring容器释放被prototype作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)

    以下的三种scope只是在web应用中才可以使用

    .request

    .session

    .global session

    使用这三种配置之前要先初始化Web配置

    http://www.cnblogs.com/njtcy/p/3328076.html

    /**
     * When used as a type-level annotation in conjunction with
     * {@link org.springframework.stereotype.Component @Component},
     * {@code @Scope} indicates the name of a scope to use for instances of
     * the annotated type.
     *
     * <p>When used as a method-level annotation in conjunction with
     * {@link Bean @Bean}, {@code @Scope} indicates the name of a scope to use
     * for the instance returned from the method.
     *
     * <p><b>NOTE:</b> {@code @Scope} annotations are only introspected on the
     * concrete bean class (for annotated components) or the factory method
     * (for {@code @Bean} methods). In contrast to XML bean definitions,
     * there is no notion of bean definition inheritance, and inheritance
     * hierarchies at the class level are irrelevant for metadata purposes.
     *
     * <p>In this context, <em>scope</em> means the lifecycle of an instance,
     * such as {@code singleton}, {@code prototype}, and so forth. Scopes
     * provided out of the box in Spring may be referred to using the
     * {@code SCOPE_*} constants available in the {@link ConfigurableBeanFactory}
     * and {@code WebApplicationContext} interfaces.
     *
     * <p>To register additional custom scopes, see
     * {@link org.springframework.beans.factory.config.CustomScopeConfigurer
     * CustomScopeConfigurer}.
     *
     * @author Mark Fisher
     * @author Chris Beams
     * @author Sam Brannen
     * @since 2.5
     * @see org.springframework.stereotype.Component
     * @see org.springframework.context.annotation.Bean
     */
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Scope {
    
        /**
         * Alias for {@link #scopeName}.
         * @see #scopeName
         */
        @AliasFor("scopeName")
        String value() default "";
    
        /**
         * Specifies the name of the scope to use for the annotated component/bean.
         * <p>Defaults to an empty string ({@code ""}) which implies
         * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}.
         * @since 4.2
         * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
         * @see ConfigurableBeanFactory#SCOPE_SINGLETON
         * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
         * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
         * @see #value
         */
        @AliasFor("value")
        String scopeName() default "";
    
        /**
         * Specifies whether a component should be configured as a scoped proxy
         * and if so, whether the proxy should be interface-based or subclass-based.
         * <p>Defaults to {@link ScopedProxyMode#DEFAULT}, which typically indicates
         * that no scoped proxy should be created unless a different default
         * has been configured at the component-scan instruction level.
         * <p>Analogous to {@code <aop:scoped-proxy/>} support in Spring XML.
         * @see ScopedProxyMode
         */
        ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
    
    }
  • 相关阅读:
    Cassandra
    【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二
    m*n matrix min rank square matrix
    Moving Computation is Cheaper than Moving Data
    SASL mechanism
    一阶 斜率 二阶 原函数的粗糙度 roughness
    Linux 虚拟内存盘
    Bloom Filters
    R Tree
    释放内存
  • 原文地址:https://www.cnblogs.com/softidea/p/4439727.html
Copyright © 2011-2022 走看看