Spring scope 配置
Scope 描述的是 Spring 容器如何新建Bean的实例,Spring的Scope有以下几种,通过@Scope来注解实现:
1. Singleton: 一个Spring容器中只有一个Bean的实例。
2. Prototype: 每次调用新建一个Bean的实例。
3. Request: Web项目中,给每一个http request新建一个Bean的实例。
4. Session: Web项目中,给每一个http session新建一个Bean的实例。
5. GlobalSession: 给每一个global http session新建一个Bean实例。
例: singleton 和 Prototype 区别:
1. 编写 Singleton 的 Bean类
package com.cz.scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; /**
* Created by Administrator on 2017/5/11.
*/
@Service
public class DemeSingletonService {
}
2. 创建 Prototyped 的 Bean类
package com.cz.scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; /**
* Created by Administrator on 2017/5/11.
*/
@Service
@Scope("prototype")
public class DemoPrototypeService {
3. 创建 Scope java配置类
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by Administrator on 2017/5/11.
*/
@Configuration
@ComponentScan("com.cz.scope")
public class ScopeConfig {
}
4. 创建测试类
package com.cz.scope; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by Administrator on 2017/5/11.
*/
public class TestScope { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
DemeSingletonService s1 = context.getBean(DemeSingletonService.class);
DemeSingletonService s2 = context.getBean(DemeSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("s1 equals s2 = " + s1.equals(s2));
System.out.println("p1 equals p2 = " + p1.equals(p2));
}
}
5. 执行结果,可以看出 single创建的两次对象是相同的,而prototype创建的两次对象是不同的
Spring scope 配置的更多相关文章
- Spring Cloud 配置服务
Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...
- Spring常用配置示例
Spring 是一款Java平台的开源框架,是为解决企业级应用程序开发的复杂性而创建的,通过良好的分层架构让开发人员能够专注于业务逻辑的开发. Spring框架是一个分层架构,由不同的模块组成,构成s ...
- Spring Cloud配置中心(Config)
Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...
- 基于注解的Spring事务配置
spring采用@Transactional注解进行事务申明,@Transactional既可以在方法上申明,也可以在类上申明,方法申明优先于类申明. 1.pom配置 包括spring核心包引入以及s ...
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- spring cloud 配置中心
1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...
- Spring MVC配置静态资源和资源包
Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...
- 简述Spring及配置
简述Spring及配置 Spring最主要的思想就是IoC(Inversionof Control,控制反转),或者成为DI(Dependency Injection,依赖注入) 一.springMV ...
- 最小可用 Spring MVC 配置
[最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...
随机推荐
- linux 下查看文件修改时间,访问时间,状态改变时间
http://blog.sina.com.cn/s/blog_6285b04e0100f4xr.html 查看文件时间戳命令:stat awk.txt File: `awk.txt' Size: 20 ...
- Docker Compose实例
采用java -jar启动 nohup java -jar web--SNAPSHOT.jar --spring.profiles.active=test --server.port= & 采 ...
- 每天一道算法题(4)——O(1)时间内删除链表节点
1.思路 假设链表......---A--B--C--D....,要删除B.一般的做法是遍历链表并记录前驱节点,修改指针,时间为O(n).删除节点的实质为更改后驱指针指向.这里,复制C的内容至B(此时 ...
- Java基础之cmd入门操作笔记
前提:jdk已安装且环境变量配置成功,参考上文jdk 安装及环境变量配置 入门操作步骤: 1.打开记事本或者notepad,编写Abc代码,具体如下: public class Abc{ pub ...
- glib-2.40编译安装
1 安装glib库所需要的依赖库: libffi-.tar.gz glib-.tar.xz 安装依赖库libffi: tar xf libffi-.tar.gz cd libffi- ./config ...
- c++中的const关键字的理解
看effective c++第二版推荐使用const,少用define.今天才发现发现这远远不够. #define定义的常量在预处理替换,debug的时候无法打印宏的,这种常量设置是有缺陷的, con ...
- bootstrap初学者模板
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> ...
- (扫码二维码不显示)php微信扫码支付sdk不能用了
解决方案: img标签中src 该为qrcode.php的绝对路径,如: 备注:微信端这个生成二维码的路径老是改,之前是http://paysdk.weixin.qq.com/example/qrco ...
- 2014-11-3 NOIP模拟赛2
NOIP 2014 水题模拟赛 (请选手务必仔细阅读本页内容) 一.题目概况 中文题目名称 数列 刷漆 排队 英文题目与子目录名 seq paint layout 可执行文件名 seq paint l ...
- Mol Cell Proteomics. |赵赟| 全面地分析个人尿蛋白质组学的变化揭示出不同的性别变化
大家好,本周分享的是发表在Molecular & Cellular Proteomics上的一篇关于人的尿蛋白质组学的文章,题目是Comprehensive analysis of indiv ...