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. React中state与props介绍与比较

    一.state 1.state的作用 state是React中组件的一个对象.React把用户界面当做是状态机,想象它有不同的状态然后渲染这些状态,可以轻松让用户界面与数据保持一致. React中,更 ...

  2. Http协议以及模拟http请求发送数据

    1 为什么要使用http协议 假设我现在有两个客户端浏览器,一个是google,一个是IE浏览器:我现在有两个服务器,一个是tomcat,一个是JBoss;在最初的情况下是:如果google要往tom ...

  3. BeanFactory和ApplicationContext的介绍

    ------------------siwuxie095                             Spring 通过一个配置文件描述 Bean 和 Bean 之间的依赖关系, 利用 J ...

  4. how to download a file with Nodejs(without using third-party libraries)用node下载文件

    创建HTTP GET请求并将其管理response到可写文件流中: var http = require('http'); var fs = require('fs'); var file = fs. ...

  5. nodejs处理页面跳转url地址的处理

    使用status函数设置状态码 router.get("/list/:id",(req,res)=>{ let id = req.params.id; res.locals. ...

  6. robot framework结合Jenkins(一)

    一.CI与Jenkins介绍: 1.持续集成(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化 ...

  7. Leetcode: 67. Add Binary

    二进制加法 https://discuss.leetcode.com/topic/33693/another-simple-java public String addBinary(String a, ...

  8. SQL笔记:基础篇

    1.BETWEEN AND (查询某个区间的数据) 例如:查询user表中年龄在15-30岁的人 SELECT * FROM user WHERE age between 15 and 30 2.IN ...

  9. LeetCode: 171 Excel Sheet Column Number(easy)

    题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...

  10. webpack@3.6.0(3)-- 优化

    本篇内容 babel配置 打包调试 第三方资源引入 静态资源的集中输出 babel配置 cnpm i -D babel-core babel-loader babel-preset-es2015 // ...