美文网首页
spring scope

spring scope

作者: 薛云龙 | 来源:发表于2016-10-15 15:01 被阅读29次
    The scope of this bean: typically "singleton" (one shared instance, which will be returned 
    by all calls to getBean with the given id), or "prototype" (independent instance resulting 
    from each call to getBean). Default is "singleton". Singletons are most commonly used, 
    and are ideal for multi- threaded service objects. Further scopes, such as "request" or 
    "session", might be supported by extended bean factories (e.g. in a web environment). 
    Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be 
    specified per concrete bean definition. Inner bean definitions inherit the singleton status 
    of their containing bean definition, unless explicitly specified: The inner bean will be a 
    singleton if the containing bean is a singleton, and a prototype if the containing bean has 
    any other scope.
    

    singleton:单例。默认对象创建时机为容器加载时机
    prototype:多例。默认对象创建时机为我们调用该对象时创建。
    request和session都是在web环境下使用。
    配置文件:

    <bean class="com.xyl.helloSpring" id="hello" lazy-init="true" scope="prototype"></bean>
    

    注解方式使用多例:@scope("prototype"),标注在类上,在装载的该类的时候,每次都会新初始化一个该类的实例。

    相关文章

      网友评论

          本文标题:spring scope

          本文链接:https://www.haomeiwen.com/subject/arznyttx.html