Bean的Scope
Bean的scope:
1、Singleton(单例): 一个Spring容器只有以这个Bean实例。
2、prototype(多例): 每次调用新建一个Bean的实例。
3、request:一个http request请求一个Bean实例。
4、Session:一个http session请求一个Bean实例。
5、GlobalSession:portal应用中有用
package com.wisely.heighlight_spring4.ch2.scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("singleton") //配置作用域单列(默认就是单列)
public class DemoSingletonService { }
package com.wisely.heighlight_spring4.ch2.scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("prototype") //配置作用域是多列
public class DemoPrototypeService { }
package com.wisely.heighlight_spring4.ch2.scope; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.wisely.heighlight_spring4.ch2.scope")
public class ScopeConfig { }
package com.wisely.heighlight_spring4.ch2.scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
DemoSingletonService demoSingletonService1 = context.getBean(DemoSingletonService.class);
DemoSingletonService demoSingletonService2 = context.getBean(DemoSingletonService.class);
System.out.println("Singleton++++"+(demoSingletonService1.equals(demoSingletonService2)));
DemoPrototypeService demoPrototypeService1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService demoPrototypeService2 = context.getBean(DemoPrototypeService.class);
System.out.println("prototype++++"+(demoPrototypeService1.equals(demoPrototypeService2)));
}
}

Bean的Scope的更多相关文章
- Spring揭秘 读书笔记 三 bean的scope与FactoryBean
本书可作为王富强所著<<Spring揭秘>>一书的读书笔记 第四章 BeanFactory的xml之旅 bean的scope scope有时被翻译为"作用域&quo ...
- spring中bean的scope属性理解
bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...
- Spring(三)之Ioc、Bean、Scope讲解
Spring容器是Spring Framework的核心.容器将创建对象,将它们连接在一起,配置它们,并管理从创建到销毁的整个生命周期.Spring容器使用DI来管理组成应用程序的组件.这些对象称为S ...
- spring bean的scope
scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象. ...
- 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )
前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...
- spring bean中scope="prototype“的作用
今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...
- Spring中bean的scope详解
如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...
- Spring中bean的scope
Spring容器中的bean具备不同的scope,最开始只有singleton和prototype,但是在2.0之后,又引入了三种类型:request.session和global session,不 ...
- Springboot的Bean的Scope
这周在项目中遇到这样一个Bug,代码大致是这样的,有一个LogEntity日志类,里面有一个InnerLog负责存储每次请求的RPCInfo相关信息, 每次请求的时候会把RPC相关信息加入到Inner ...
随机推荐
- spark-sql中的DataFrame文件格式转储示例
SparkConf sparkConf = new SparkConf() // .setMaster("local") .setAppName("DataFrameTe ...
- 微信小程序点击图片全屏
作为一个只懂简单HTML,jQuery,JS的web后台开发者,最近在学习小程序开发,现在将小程序的点击全屏功能的相关内容记录下来.如果有不对的地方或者有更简单的方法,请留言指教 0_0~ .js 文 ...
- mongodb 3.2 分片 + 副本集
从图中可以看到有四个组件:mongos.config server.shard.replica set. mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加 ...
- container_of 例子说明
很早前之前看的linux内核,一直想把container_of记录一下,趁今天想起就记录一下: 内核中的描述 /** * container_of - cast a member of a struc ...
- JedisCluster简单使用
项目中因为一些原因需要用到缓存,之前没有接触过,在此做一些简单的使用记录. 1.jedis在项目中依赖 <dependency> <groupId>redis.clients& ...
- python数据结构-如何为元组中的每个元素命名
如何为元组中的每个元素命名 简述 当对象数据格式固定时,用元组比列表更节省内存空间, 我们使用索引访问元组元素,但是这种访问方式会降低程序的可读性. 举个栗子 对于学生的信息,我们有固定的数据格式,我 ...
- Axure RP 8过期,用户名和序列号(注册码)
用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+tqEV4LG 用户名:aaa注册码:2GQrt5 ...
- 动物管理员--zooKeeper-01
ZooKeeper集群角色介绍: 最典型集群模式:Master/Slave 模式(主备模式).在这种模式中,通常 Master 服务器作为主服务器提供写服务,其他的 Slave 服务器从服务器通过异步 ...
- Zookeeper应用之——栅栏(barrier)
Zookeeper应用之——栅栏(barrier) 栅栏(barrier)简介 barrier的作用是所有的线程等待,知道某一时刻,锁释放,所有的线程同时执行.举一个生动的例子,比如跑步比赛,所有 运 ...
- Subverion仓库迁移知识点整理
目录 dump远程仓库内容到本地 可能碰到的问题 将dumpFile导入到本地仓库 可能碰到的问题 如何获取控制台信息 最近在工作上提出了一个需求,需要将各个团队的Subversion仓库进行集中管理 ...