【Spring】Spring常用配置-Bean的Scope
来源:互联网 发布:淘宝银饰店哪家是正品 编辑:程序博客网 时间:2024/05/16 15:59
转载请注明出处:http://blog.csdn.net/qq_26525215
本文源自【大学之旅_谙忆的博客】
分析
Scope(范围)描述的是Spring容器如何新建Bean的实例的。可以简单的理解成Bean的作用范围!
Spring的Scope有以下的几种,可以通过@Scope注解来实现。
1、singleton:一个Spring容器中只有一个Bean的实例。 这是Spring的默认配置,也就是不写@Scope("singleton"),全容器共享一个实例。2、prototype:每次调用都会新建一个Bean的实例。3、request:Web项目中,给每一个http request新建一个Bean实例。 也就是每一个request请求,都会新建一个Bean。4、session:Web项目中,给每一个http session新建一个Bean实例。 也就是同一个session访问的请求,都是同一个Bean。5、globalSession:这个只在portal应用中有用,给每一个global http session新建一个Bean实例。
在Spring Batch中还有一个Scope是使用@StepScope的,这里就不介绍了。以后会有博客提到。
现在要去了解的,请自行谷歌。
下面的实例是简单的演示默认的singleton和prototype,分别从Spring容器中获得2次Bean,分别用==与equals判断Bean的实例是否相等!
示例
singleton的Bean
package cn.hncu.p2_1_1Scope;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/11. * Time: 上午 11:09. * Explain:Singleton---默认Spring-Scope */@Service//默认@Scope为Singleton-相当于添加//@Scope("singleton")public class DemoSingletonService {}
prototype的Bean
package cn.hncu.p2_1_1Scope;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/11. * Time: 上午 11:23. * Explain:编写Prototype的Bean */@Service@Scope("prototype")public class DemoPrototypeService {}
配置类
package cn.hncu.p2_1_1Scope;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/11. * Time: 上午 11:42. * Explain:配置类 */@Configuration@ComponentScan("cn.hncu.p2_1_1Scope.")public class ScopeConfig {}
运行
package cn.hncu.p2_1_1Scope;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/11. * Time: 上午 11:43. * Explain:运行类 */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class); DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); DemoSingletonService s1 = context.getBean(DemoSingletonService.class); DemoSingletonService s2 = context.getBean(DemoSingletonService.class); System.out.println("p1.equals(p2):"+p1.equals(p2));//false System.out.println("p1==p2:"+(p1==p2));//false System.out.println("s1.equals(s2):"+s1.equals(s2));//true System.out.println("s1==s2:"+(s1==s2));//true }}
运行结果
项目链接—具体包:
https://github.com/chenhaoxiang/Java/tree/master/springBoot/src/main/java/cn/hncu/p2_1_1Scope
本文章由[谙忆]编写, 所有权利保留。
转载请注明出处:http://blog.csdn.net/qq_26525215
本文源自【大学之旅_谙忆的博客】
1 1
- 【Spring】Spring常用配置-Bean的Scope
- 04-SpringBoot——Spring常用配置-Bean的Scope
- Spring Bean的Scope
- spring bean的scope
- Spring Bean的Scope
- Spring Bean的scope
- spring bean的scope
- Spring中Bean的scope配置
- spring之bean的Scope
- spring中bean的scope
- Spring中Bean的Scope
- spring bean的scope属性
- spring bean的scope属性
- spring bean的scope属性
- spring中bean的scope
- Spring入门-----Bean的Scope
- spring bean scope模式配置的一些思考
- Spring配置<bean>时加scope="prototype"的原因
- mysql event
- 【PHP笔记】PHP实现验证码
- TelephonyManager监听电话状态
- 400. Nth Digit
- 碰到the authenticity of host github.com can't be established问题怎么解决
- 【Spring】Spring常用配置-Bean的Scope
- Linux下查看MySQL的安装路径
- 一次意外的惊喜
- webstrom如何使用svn插件进行管理
- 操作系统——操作系统启动
- 自旋锁、排队自旋锁、MCS锁、CLH锁
- uc/os iii(一) 系统时钟及其中断服务函数
- Java基础(一)
- 十进制转换为二进制的简洁算法