Scope描述的是Spring容器如何新建Bean的实例的。

1> Singleton: 一个Spring容器只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例。

2> Prototype: 每次调用都新建一个Bean的实例。

3> Request: Web 项目中,给每一个 http request 新建一个Bean实例。

4> Session: Web项目中,给每一个 http session 新建一个Bean实例。

5> GlobalSession: 这个只在portal应用中有用,给每一个global http session 新建一个Bean实例。

But ....这里要说的是Singleton 和 Prototype的区别:

1.新建Spring Boot项目,有如下类:

2. POM 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
package com.sbia.ch2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.sbia.ch2")
public class ConfigurationClass { }
package com.sbia.ch2;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("prototype")
public class DemoPrototypeService { }
package com.sbia.ch2;

import org.springframework.stereotype.Service;

@Service// 默认为 Singleton,相当于@Scope("singleton")
public class DemoSingletonService { }
package com.sbia.ch2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ConfigurationClass.class);
context.refresh(); DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("S1 与 S2是否相等: " + s1.equals(s2));
System.out.println("P1 与P2是否相等: " + p1.equals(p2)); } }

Run Main.class as Java Application,get the result like below:

Singleton and Prototype Bean Scope in Spring的更多相关文章

  1. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  2. 【Spring】bean的作用域(@Scope) - singleton、prototype

    已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...

  3. [Spring] Bean Scope Singleton cs Prototype

    We can define a class to be Singleton or Prototype. If the class was defined as Prototype, then ever ...

  4. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

  5. Spring Bean Scope 有状态的Bean 无状态的Bean

    http://blog.csdn.net/anyoneking/article/details/5182164 在Spring的Bean配置中,存在这样两种情况: <bean id=" ...

  6. (转)Spring Bean Scope 有状态的Bean 无状态的Bean

    有状态会话bean   :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一 ...

  7. Spring学习(二):Spring支持的5种Bean Scope

    序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...

  8. Spring标签之Bean @Scope

    @Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...

  9. 使用lookup-method解决singleton bean依赖prototype bean的问题

    在Spring里面,当一个singleton bean依赖一个prototype bean,那么,因为singleton bean是单例的,因此prototype bean在singleton bea ...

随机推荐

  1. wxWidgets编译和在VC 6.0中的配置

    1. 安装  运行wxMSW-2.8.3-Setup1.exe,将之安装到不带空格符号的目录中,本例为C:/wxWidgets-2.8.3:   2. 编译和配置 (1) 用VC6.0编译  进入C: ...

  2. WPF 3D模型的一个扩展方法

    原文:WPF 3D模型的一个扩展方法 在WPF 3D中,我们常常需要改变一个ModelVisual3D对象的颜色. 先说说ModelVisual3D,本质上3D模型都是由一个个的三角形构成的,并且经过 ...

  3. arcserver开发小结(一)

    一.关于属性查询 由于要做属性查询,又重新玩起了arcmap中的select by attribute,有很多自己当初玩弄arcmap多年还不是很清楚的东西 1,字段名 (1)file geodata ...

  4. ShopNC本地生活o2o网站的源代码,没有域名限制

    较前某VIP源代码论坛分享了套ShopNC本地生活o2o站点系统.下载过来却发现根本不能用,所以一直没分享出来.今天咱们这边分享的这套ShopNC本地生活o2o站点源代码,无不论什么的限制,直接ins ...

  5. linux_ linux下查看最消耗CPU、内存的进程 20

    1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...

  6. [Android] 环境优化配置Android Studio发展NDK

    ======================================================== 作者:qiujuer 博客:blog.csdn.net/qiujuer 站点:www. ...

  7. symfony 数据库表生成实体、迁移数据库

    从数据库表生成实体 1. 由数据库生成模型: php bin/console doctrine:mapping:convert --from-database yml D:\db\ D:\test_b ...

  8. 【已解决】Android Studio下,gradle project sync failed 错误

    原文:[已解决]Android Studio下,gradle project sync failed 错误 Android studio下突然报错 gradle project sync failed ...

  9. mfc动态显示图片

    参考:https://blog.csdn.net/pudongdong/article/details/69396600 之前写过win32动态显示图片的博客,这次用到了mfc.原理是一样的. OnI ...

  10. 关于DexOpt: not all deps represented

    最近在做android BSP 4.2的时候遇到一个BUG,编译user 版本的时候,系统刷进手机里面去,无限循环在开机动画,编译userdebug 刷机进去的时候发现正常,于是我先回滚到正常的版本, ...