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. 我推荐的一些C\C++书籍

    原文链接: 我推荐的一些C\C++书籍 人们常常问我有什么C++和编程的书籍推荐,也许是因为我在PowerDNS有一个关于"编写可读性良好的C++代码"的演讲.这篇博文可以作为我对 ...

  2. Matlab零碎知识

    1.不定积分的求取 int syms x;%为自变量 f=x.^2; s=int(f,x); 其中显示辅助函数simple()和pretty()

  3. Hive 进阶

    两种情况下不走map-reduce: 1. where ds >' ' //ds 是partition 2. select * from table //后面没有查询条件,什么都没有 1.建表 ...

  4. Spring JdbcTemplate+JdbcDaoSupport实例(和比较)

    首先,数据库是这样的,很简单. 当然,要引入spring的包,这里我全部导入了,省事. applicationContext.xml是这样的: <?xml version="1.0&q ...

  5. htmlparser API

    htmlparser所有的filter htmlparser所有的Tags htmlparser API: http://htmlparser.sourceforge.net/javadoc/inde ...

  6. JavaScript中匿名函数循环传参数(不触发函数的执行)

    我们都知道定义函数的方式有两种,一种是函数声明,另一种是函数表达式,函数声明的语法是这样的: function functionName(arg0, arg1, arg2) { // 函数体 } 函数 ...

  7. nodebrew

    创建: 2019/05/30 完成: 2019/05/30  安装  安装 curl -L git.io/nodebrew | perl - setup 更新nodebrew nodebrew sel ...

  8. oracle语句解析顺序

  9. 清北刷题冲刺 10-30 p.m

    少女 #include<iostream> #include<cstdio> #include<queue> #include<cstdlib> #de ...

  10. 唯快不破:Web 应用的 13 个优化步骤

    时过境迁,Web 应用比以往任何时候都更具交互性.搞定性能可以帮助你极大地改善终端用户的体验.阅读以下的技巧并学以致用,看看哪些可以用来改善延迟,渲染时间以及整体性能吧! 更快的 Web 应用 优化 ...