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. Interllij IDEA中启动web项目

    1.在IDEA中打开你的Web应用,点击一下绿色三角形左边的框框,然后在弹出框上选择Edit Configurations,会弹出一个配置面板. 2.在弹出的面板中我们点击Defaults,然后找到T ...

  2. 从光盘安装ubuntu系统

    参考博客: https://www.jianshu.com/p/7929e4911206

  3. python框架之Django(13)-admin组件

    使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLED_APPS ...

  4. es6合并数组...

    加了...和不加...是不一样,...会把原来数组拆开

  5. Linux操作系统加固

    1. 账号和口令 1.1 禁用或删除无用账号 减少系统无用账号,降低安全风险. 操作步骤 使用命令 userdel <用户名> 删除不必要的账号. 使用命令 passwd -l <用 ...

  6. dll静态调用和动态调用

    动态链接库有2种连接方式,一种是通过库直接加入(又叫隐式加载或载入时加载),一种是在运行时加入.后者很好理解,比如LoadLibrary(),GetProcAddress()获取想要引入的函数,使用完 ...

  7. python selenium webdriver入门基本操作

    python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...

  8. python基础(十三) cmd命令调用

    python cmd命令调用 关于python调用cmd命令: 主要介绍两种方式: 1.python的OS模块. OS模块调用CMD命令有两种方式:os.popen(),os.system(). 都是 ...

  9. TP连接数据库报错:SQLSTATE[HY000] [2002] No such file or directory

     连接数据库报错:“SQLSTATE[HY000] [2002] No such file or directory”. 出现这个问题的原因是PDO无法找到mysql.sock或者mysqld.soc ...

  10. 记录 用tiny6410 j-link eclipse 在线调试裸机代码leds

    1.nand flash烧写uboot 并且启动nandflash uboot,用来初始化6410,进入uboot命令行界面 2.在terminal中输入JLinkGDBServer -device ...