Spring IOC之bean的scope属性值:prototype与singleton
2017-06-08 14:13
513 查看
在spring2.0中,bean没有“singleton”这个属性,而是在“scope”中对它进行设定。“scope”可以设定为“singleton”和“prototype”,默认情况下是“singleton”即和原先的“singleton=true”性质一样,如果要实现非单例模式则将“scope”设定为“prototype”,即和原先版本的“singleton=false”一样。
具体配置方法:
spring 1.*
<bean id="hibernateSessionFactory" class="org.hibernate.admin.component.HibernateSessionFactory" init-method="init"
destroy-method="dispose"singleton="true">
Spring 2.0
<!--spring-beans-2.0.dtd/xsd does not support singleton="true"/"false" anymore. Use scope="singleton/"prototype" instead!-->
<bean id="hibernateSessionFactory" class="org.hibernate.admin.component.HibernateSessionFactory" init-method="init" destroy-method="dispose"scope="singleton">
Bean以两种形态存在:singletons形式和prototypes形式。当bean以singletons形态存在时,BeanFactory只管理一个共享的实例。所有对这个特定bean的实例请求,都导致返回这个唯一bean实例的引用。当bean以prototype形态存在时,每次对这个bean的实例请求都会导致一个新的实例的创建。当用户需要不受其他用户对象影响的对象或有类似的需求时,这是一个较理想的解决办法。Bean默认是以singleton形态存在的,除非你另外显式加以指定。
Spring的Singleton主要是针对"一个IoC容器维持一个Bean实例"而言的。与设计模式上谈到的Singleton不同,设计模式上谈到的Singleton,是指对每个ClassLoader所载入的类产生一个实例。Spring2.0中,scope除了可以设置"singleton"
与"prototype"之外,针对Web应用程序环境,还可以设置"request"、"session"与"globalSession",分别表示请求阶段、会话阶段与基于Portlet的Web应用程序会话阶段。
如果使用BeanFactory,只有在使用getBean()方法真正取得Bean时,才会做实例化的动作。如果使用ApplicationContext,则会预先针对Bean定义文件的内容,将所有的Bean实例化。当然如果你想要延迟加载功能,则可以在<bean>上设置"lazy-init"属性为true。
Webapp的Action不是线程安全的,要求在多线程环境下必须是一个线程对应一个独立的实例,不能使用singleton。所以,我们在Spring配置Action的Bean时,需要加上属性scope=”prototype”或singleton=”false”。
具体配置方法:
spring 1.*
<bean id="hibernateSessionFactory" class="org.hibernate.admin.component.HibernateSessionFactory" init-method="init"
destroy-method="dispose"singleton="true">
Spring 2.0
<!--spring-beans-2.0.dtd/xsd does not support singleton="true"/"false" anymore. Use scope="singleton/"prototype" instead!-->
<bean id="hibernateSessionFactory" class="org.hibernate.admin.component.HibernateSessionFactory" init-method="init" destroy-method="dispose"scope="singleton">
Bean以两种形态存在:singletons形式和prototypes形式。当bean以singletons形态存在时,BeanFactory只管理一个共享的实例。所有对这个特定bean的实例请求,都导致返回这个唯一bean实例的引用。当bean以prototype形态存在时,每次对这个bean的实例请求都会导致一个新的实例的创建。当用户需要不受其他用户对象影响的对象或有类似的需求时,这是一个较理想的解决办法。Bean默认是以singleton形态存在的,除非你另外显式加以指定。
Spring的Singleton主要是针对"一个IoC容器维持一个Bean实例"而言的。与设计模式上谈到的Singleton不同,设计模式上谈到的Singleton,是指对每个ClassLoader所载入的类产生一个实例。Spring2.0中,scope除了可以设置"singleton"
与"prototype"之外,针对Web应用程序环境,还可以设置"request"、"session"与"globalSession",分别表示请求阶段、会话阶段与基于Portlet的Web应用程序会话阶段。
如果使用BeanFactory,只有在使用getBean()方法真正取得Bean时,才会做实例化的动作。如果使用ApplicationContext,则会预先针对Bean定义文件的内容,将所有的Bean实例化。当然如果你想要延迟加载功能,则可以在<bean>上设置"lazy-init"属性为true。
Webapp的Action不是线程安全的,要求在多线程环境下必须是一个线程对应一个独立的实例,不能使用singleton。所以,我们在Spring配置Action的Bean时,需要加上属性scope=”prototype”或singleton=”false”。
相关文章推荐
- Spring IOC之bean的scope属性值:prototype与singleton
- spring IOC之Bean的scope属性值:prototype与singleton
- Java Spring Tutorial -- Singleton and Prototype Bean Scopes
- Spring中Bean的scope的singleton和prototype使用场景
- Spring Bean Scope Singleton Prototype 相互引用说明
- spring bean scope=prototype/singleton的差别
- spring配置文件中bean中scope属性prototype和singleton
- 【Spring】bean的作用域(@Scope) - singleton、prototype
- 详解Spring中bean的scope singleton prototype request...
- 《转》详解Spring中bean的scope singleton prototype request...
- spring中scope的singleton和prototype区别
- spring 中属性scope 的prototype(有状态)和singleton(无状态)
- spring scope="prototype" 和scope="singleton"区分
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- spring scope prototype与singleton区别
- Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期
- Spring(三)之Ioc、Bean、Scope讲解
- spring开发_spring中Bean的作用域_singleton_prototype
- spring scope prototype与singleton区别
- 关于spring容器scope="singleton"或scope=“prototype”理解