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 配置的更多相关文章

  1. Spring Cloud 配置服务

    Spring Cloud 配置服务 1. 配置服务简介 产生背景: 传统开发中,我们通常是将系统的业务无关配置(数据库,缓存服务器)在properties中配置,在这个文件中不会经常改变,但随着系统规 ...

  2. Spring常用配置示例

    Spring 是一款Java平台的开源框架,是为解决企业级应用程序开发的复杂性而创建的,通过良好的分层架构让开发人员能够专注于业务逻辑的开发. Spring框架是一个分层架构,由不同的模块组成,构成s ...

  3. Spring Cloud配置中心(Config)

    Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...

  4. 基于注解的Spring事务配置

    spring采用@Transactional注解进行事务申明,@Transactional既可以在方法上申明,也可以在类上申明,方法申明优先于类申明. 1.pom配置 包括spring核心包引入以及s ...

  5. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  6. spring cloud 配置中心

    1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...

  7. Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  8. 简述Spring及配置

    简述Spring及配置 Spring最主要的思想就是IoC(Inversionof Control,控制反转),或者成为DI(Dependency Injection,依赖注入) 一.springMV ...

  9. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

随机推荐

  1. 使用pygame制作一个简单的游戏

    翻译自Will McGugan的<Beginning Game Development with Python and Pygame –From Novice to Professional&g ...

  2. 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree

    ###https://www.biostars.org/p/195758/ Left reads: Input : 49801387 Mapped : 46258301 (92.9% of input ...

  3. 34、NCBI的子库名称

    转载:http://fhqdddddd.blog.163.com/blog/static/1869915420107188527933/ 参考资料: http://www.ncbi.nlm.nih.g ...

  4. Centos下添加/删除用户

    useradd具体参数 [root@yhwang ~] useradd -h Usage: useradd [options] LOGIN useradd -D useradd -D [options ...

  5. kingadmin

    kingadmin 是一个模拟 Django admin 开发的后台管理系统,可以用来嵌套在其他的项目中作为单独的 app 程序存在. 执行流程 1.项目启动,开始执行 app_setup.py 文件 ...

  6. CSS类名命名规则

    CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或#content 容器,用于最外层 #layout 布局 #head, #header 页头部分 ...

  7. Postman使用-2

    转载:https://www.cnblogs.com/yunman/p/7884537.html Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 接口请求流程 一.g ...

  8. superset 错误解决

    访问superset localhost:8088   securety->list Role 报错 xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx ...

  9. Codeforces - 77B - Falling Anvils - 几何概型

    https://codeforc.es/contest/77/problem/B 用求根公式得到: \(p-4q\geq0\) 换成熟悉的元: \(y-4x\geq0\) 其中: \(x:[-b,b] ...

  10. java知识点积累(二)

    4.条件运算符(三元运算符): String type = score<60?"不及格":"及格"; int i = (string=="hel ...