注:原创作品,转载请注明出处
Spring提供五种选择让用户决定bean的作用范围:
1,Singleton:单例模式,在整个Spring ioc中。使用singleton定义的bean将只有一个实例。
例如:
<bean id="userDao" class="com.supan.dao.imp.UserDaoImp" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
测试java代码:
public void testSpringScope()
{
UserDao ud1 = (UserDao)ctx.getBean("userDao");
UserDao ud2 = (UserDao)ctx.getBean("userDao");
System.out.println(ud1 == ud2);
}
输出:true
2,prototype 原型模式:每次通过spring容器的getBean都将产生一个新的实例
例如:
spring配置文件:
<bean id="userDao" class="com.supan.dao.imp.UserDaoImp" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
java测试代码:
/**
* <测试spring配置bean的属性scopt的作用>
*/
public void testSpringScope()
{
UserDao ud1 = (UserDao)ctx.getBean("userDao");
UserDao ud2 = (UserDao)ctx.getBean("userDao");
System.out.println(ud1 == ud2);
}
输出:false
3,request:对于每次http请求,使用request定义的bean都将产生一个新的实例。注:只有在web容器使用spring时,这个作用域才生效。
<bean id="LoginAction" class="com.supan.LoginAction" scope="request">
4,session:对于每次http session请求,使用session定义的bean都将产生一个新的实例。。注:只有在web应用中使用spring,此作用域才能生效。
由于本机上没有j2ee环境,没有实例展出。
5,global session:每个全局的Http session对应一个bean实例,典型情况下,仅在使用portletcontext的时候才有效。注:只有在web应用中使用spring,此作用域才能生效。
由于本机上没有j2ee环境,没有实例展出。
最后写一点:使用经验:
1, Sington是单例模式,由ioc容器管理,不是线程安全的。
若类是无状态的,那么sington性能要高一点,因为只有一个实例。
若类是有状态的,那么必须把scope设置成prototype。
2, 在ssh2项目中struts的action是有状态的,必须显示设置成scope="prototype",prototype是原型模式,每次action请求过来都会创建一个新的action。
而对于dao的实现类,推介使用scope = "sington" 因为这些类没有状态,
3,比较常用的是singleton,和prototype两种作用域。
4,spring默认的作用域是singleton(这样最省系统资源),如非必要,请不要随便用prototype作用域(比较消耗系统资源)
5,在使用request,session,global session需要在spring中额外的配置。
第一种配置方法(Servlet2.4以后):
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
第一种配置方法(Servlet2.4以前):
<filter>
<filter-name>requestContextFilter></filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
分享到:
相关推荐
spring bean 的作用域(scope), SPringle bean的作用域
详解Spring中bean的scope以后使用,如何使用spring的作用域:
创建SpringBean配置工具类(安全)如: <bean id=... scope="prototype"></bean>
我的博客的spring之bean的Scope 的 示例代码。 包含了一个web工程(包括lib包)。但是里面也有可以执行的main函数。 环境是eclipse,spring 是 4.1.5
Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml 1.2 实体类JavaBean 1.2.1 User类 1.3 当Scope="singleton"时 1.4 当 Scope="singleton" 且 lazy-init="true" 时 1.5 当scope=...
Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean
Spring Bean 的作用域之间有什么区别:Bean的作用域: 可以通过scope 属性来指定bean的作用域 ①singleton: 默认值。当IOC容器
Bean元素有一个scope属性,用于定义Bean的作用域,该属性有如下五个值: 1>singleton: 单例模式,在整个spring IOC容器中,单例模式作用域的Bean都将只生成一个实例。一般Spring容器默认Bean的作用域为singleton ...
第一天内容:Spring框架简介(EJB、JMX、Spring核心功能、Spring模块详解、Spring重要概念(容器)、Spring容器初始化的整个流程、Spring后处理器),IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性...
Spring系列第4篇:xml中bean定义详解(-)Spring系列第5篇:创建bean实例这些方式你们都知道?Spring系列第6篇:玩转bean scope,避免跳坑里!Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入...
单例的对象是在加载spring容器时就创建了,且此后你在同一个bean获取资源时,用getBean()方法得到的对象都是同一个地址值的对象 prototype:,设定创建出的对象保存在spring容器中,是一个非单例的对象 非单例的...
NULL 博文链接:https://diaochenlong2.iteye.com/blog/1831285
NULL 博文链接:https://listen-raining.iteye.com/blog/2128694
在Spring配置文件中,可以使用<bean>元素的scope属性,将Bean的作用域定义成singleton。 singleton作用域 singleton是Spring容器默认的作用域,当Bean的作用域为singleton时,Spring容器就只会存在一个共享的Bean...
首先定义一个简单的类 import lombok.Getter; import lombok.Setter; @Getter @Setter public class HelloMessageGenerator { private String message;... return getClass().getSimpleName() + "@" + Integer....
通过有参构造创建 bean;给 bean 注入集合;scope 作用域;Spring 的继承;Spring 的依赖;Spring 的 p 命名空间;Spring 的工厂方法;IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的...
ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...
创建对象的模式(使用范围):scope控制,可以使用singleton和prototype d.初始化:<bean init-method=""/> e.资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转...
3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean时机 15 Lazy-init初始化bean的...
创建对象的模式(使用范围):scope控制,可以使用singleton和prototype d.初始化:<bean init-method=""/> e.资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转...