04-SpringBoot——Spring常用配置-Bean的Scope
来源:互联网 发布:windows 10 to go教程 编辑:程序博客网 时间:2024/05/30 04:47
Spring常用配置-Bean的Scope
【博文目录>>>】
【项目源码>>>】
【Bean的Scope】
Scope 描述的是Spring 容器如何新建Bean 的实例的。Spring 的Scope 有以下几种,通过@Scope 注解来实现。
1. Singleton :一个Spring 容器中只有一个Bean 的实例,此为Spring 的默认配置,全容器共享一个实例。2. Prototype :每次调用新建一个Bean 的实例。3. Request: Web 项目中,给每一个http request 新建一个Bean 实例。4. Session: Web 项目中,给每一个http session 新建一个Bean 实例。5. Global Session :这个只在portal 应用中有用,给每一个global http session 新建一个Bean实例。6. 在Spring Batch 中还有一个Scope 是使用@StepScope
【代码实现】
package com.example.spring.framework.scope;import org.springframework.stereotype.Service;/** * 默认为Singleton ,相当于@Scope(“singleton”) * Author: 王俊超 * Date: 2017-07-10 22:38 * All Rights Reserved !!! */@Servicepublic class DemoSingletonService {}
package com.example.spring.framework.scope;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Service;/** * Author: 王俊超 * Date: 2017-07-10 22:38 * All Rights Reserved !!! */@Service@Scope("prototype")public class DemoPrototypeService {}
package com.example.spring.framework.scope;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Author: 王俊超 * Date: 2017-07-10 22:39 * All Rights Reserved !!! */@Configuration@ComponentScan("com.example.spring.framework.scope")public class ScopeConfig {}
package com.example.spring.framework.scope;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Author: 王俊超 * Date: 2017-07-10 22:40 * All Rights Reserved !!! */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoSingletonService s1 = context.getBean(DemoSingletonService.class); DemoSingletonService s2 = context.getBean(DemoSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class); DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("s1与s2是否相等:" + s1.equals(s2)); System.out.println("p1与p2是否相等:" + p1.equals(p2)); context.close(); }}
【运行结果】
阅读全文
1 0
- 04-SpringBoot——Spring常用配置-Bean的Scope
- 【Spring】Spring常用配置-Bean的Scope
- 06-SpringBoot——Spring常用配置-Bean的初始化和销毁
- 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"的原因
- spring之bean的Scope
- spring中bean的scope
- Spring中Bean的Scope
- spring bean的scope属性
- spring bean的scope属性
- spring bean的scope属性
- 关于Rigidbody,Collider和CharacterController三者之间的关系和用法的总结
- python012 Python3 编程第一步
- PAT甲级1007. Maximum Subsequence Sum
- python013 Python3 条件控制
- U盘安装CentOS 7终极方案,简单有效
- 04-SpringBoot——Spring常用配置-Bean的Scope
- python013 Python3 循环语句
- httpclient4.5入门教程之连接管理
- MYSQL安装和卸载图解
- Appium环境搭建
- 算法一
- spring cloud Ribbon
- 基于node的智能家居
- Java 8 forEach使用