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

  1. Spring揭秘 读书笔记 三 bean的scope与FactoryBean

    本书可作为王富强所著<<Spring揭秘>>一书的读书笔记  第四章 BeanFactory的xml之旅 bean的scope scope有时被翻译为"作用域&quo ...

  2. spring中bean的scope属性理解

    bean的scope属性有prototype,singleton,request, session几个属性 spring和struts2整合的时候,struts2的action要配置成scope=&q ...

  3. Spring(三)之Ioc、Bean、Scope讲解

    Spring容器是Spring Framework的核心.容器将创建对象,将它们连接在一起,配置它们,并管理从创建到销毁的整个生命周期.Spring容器使用DI来管理组成应用程序的组件.这些对象称为S ...

  4. spring bean的scope

    scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象. ...

  5. 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )

    前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...

  6. spring bean中scope="prototype“的作用

    今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...

  7. Spring中bean的scope详解

    如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...

  8. Spring中bean的scope

    Spring容器中的bean具备不同的scope,最开始只有singleton和prototype,但是在2.0之后,又引入了三种类型:request.session和global session,不 ...

  9. Springboot的Bean的Scope

    这周在项目中遇到这样一个Bug,代码大致是这样的,有一个LogEntity日志类,里面有一个InnerLog负责存储每次请求的RPCInfo相关信息, 每次请求的时候会把RPC相关信息加入到Inner ...

随机推荐

  1. [bash][awk] bash下使用awk方便的列求和

    这么多年,始终在用awk进行文本处理.但是一直没有好好的学习awk的语法.所以很多情况都是知其然,不知其所以然. 如今,亦如此.先记下来如下,以后有时间系统的学习一下awk的语法. ┬─[tong@T ...

  2. C# 如何使用长度来切分字符串

    参考网址:https://blog.csdn.net/yenange/article/details/39637211 using System; using System.Collections.G ...

  3. python获取一年所有的日期

    python获取一年所有的日期 自动识别闰年. import arrow def isLeapYear(years): ''' 通过判断闰年,获取年份years下一年的总天数 :param years ...

  4. __call__方法和Flask中 first_or_404

    1.__call__方法: 在一个类的实例中,函数一般都是可调用的对象: __call__方法时魔法方法,该方法允许程序员创建可调用的对象(实例),默认情况下是不会触发,也就是说,大多数实例是不可被调 ...

  5. 要学的javaee技术

    mybatis.hibernate.spirng MVC.freemarker.zookeeper.dubbo.quartz的技术框架:NoSQL技术ehcache.memcached.redis等: ...

  6. python列表常用内建方法

    python列表常用内建方法: abc = ['a',1,3,'a'] #abc.pop(1) #删除索引1的值.结果['a', 3] #abc.append([123]) #结果:['a', 1, ...

  7. beego 初体验 - 基础模块 - session, cookie

    beego 内建 session 模块 首先,需要在项目中开启 Session,两种方式,以编码的方式或配置 这是代码实现: 配置文件: 这是调用:

  8. Django简单实例

    一.前戏-MVC框架与MTV框架 1.MVC MVC(Model View Controller),是一种软件工程开发的架构模式,不仅适用于软件开发,而且适用于其他广泛的设计和组织工作,主要思想通过分 ...

  9. Rdlc 参数问题

    需要sql中的参数自动在生成报表的时候生成的方法: 例如有这样的sql需要自动生成:BeginTime 和 EndTime 两个参数: DECLARE @BeginTime DATETIME='201 ...

  10. SpringIOC和AOP原理 设计模式

    SpringIOC的特点 在接触Spring的过程中,听到最多的无非两个名词,一个是控制反转一个是依赖注入.实际这是一个意思,控制反转代表原来由程序本身去控制对象之间的依赖关系的这种格局被反转了,通过 ...