转载:https://www.iteye.com/blog/wiselyman-2210377

3.1 scope

  • scope描述spring容器是怎么样新建类的实例的(bean);
  • 在spring中默认的scope是singleton,这意味着无论你在程序中多少地方使用这个bean,它们都共享唯一个实例;
  • spring内置的scope有如下几个:
    • singleton:一个spring容器中只有一个bean的实例;
    • prototype:每次调用新建一个bean的实例;
    • request:web项目中,每一个http request,新建一个bean实例;
    • session:web项目中,每一个http session,新建一个bean实例;
    • globalSession:这个只在portal应用中有用,每一个global http session,新建一个bean实例

3.2 演示

3.2.1 新建scope为singleton的类

package com.wisely.scope;

import org.springframework.stereotype.Service;

@Service//默认scope为singleton
public class DemoSingletonService { }

3.2.2 新建scope为prototype的类

package com.wisely.scope;

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

3.2.3 测试

package com.wisely.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.scope");
//分别从spring容器里获得两次实例,为singleton时共享一个实例,用prototype是又新建了一个实例
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));
context.close();
} }

输出

s1与s2是否相等:true
p1与p2是否相等:false

03点睛Spring4.1-scope的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  4. 11点睛Spring4.1-Property Editor

    11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...

  5. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  6. Spring知识点回顾(03)Bean的 Scope

    sigleton prototype request session globalsession stepscope

  7. 03点睛Spring MVC 4.1-REST

    转发:https://www.iteye.com/blog/wiselyman-2214290 3.1 REST REST:Representational State Transfer; REST是 ...

  8. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

  9. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

随机推荐

  1. YAML_10 把监听端口是8080的Apache服务全部停止

    ansible]# vim ad.yml --- - hosts: cache   remote_user: root   tasks:     - shell: netstat -atunlp  | ...

  2. learning java 访问文件和目录

    import java.io.File; import java.io.IOException; public class FileTest { public static void main(Str ...

  3. 83: 模拟赛 树形dp

    $des$ $sol$ 维护每个点的子树中的信息以及非子树的信息 $code$ #include <bits/stdc++.h> using namespace std; #define ...

  4. read()和write(),读和写的优化。

    读和写的优化在输入数据后输出数据十分多的情况下是十分有用的,比scanf和printf也要快. 读: int read(){ ; ; char c=getchar(); '){ if(c=='-') ...

  5. 如何选择梯度下降法中的学习速率α(Gradient Descent Learning Rate Alpha)

    梯度下降算法的任务是寻找参数θ,使之能够最小化损失函数. 那么梯度下降法中的学习速率α应该如何选择呢?通常我们画出损失函数随迭代次数增加而变化的曲线. 可能会得到如下的一条曲线,x轴表示迭代次数,y轴 ...

  6. 【一起来烧脑】一步学会TypeScript入门

    [外链图片转存失败(img-rmJXMGFs-1563388353181)(https://upload-images.jianshu.io/upload_images/11158618-dd813e ...

  7. Pytest权威教程13-Fixture方法及测试用例的参数化

    目录 Fixture方法及测试用例的参数化 @pytest.mark.parametrize:参数化测试函数 基本的pytest_generate_tests例子 更多示例 返回: Pytest权威教 ...

  8. 性能测试JMeter应用篇---同线程组、跨线程组实现token共用

    方式一:将token值取出,设为全局变量,同线程组内可共用token值 1.登录请求如下: 2.从登录请求返回json字符串中提取token值,保存为变量token_0: 3.将token设置为全局变 ...

  9. docker理论 Cgroup namespace 各种隔离

    耦合 是指两个或两个以上的体系或者两种运动形式间通过相互作用而批次影响以至联合起来的现象. Nginx与apache 在同一台服务器运行都占用80端口,起冲突这是我们修改其中一个端口为8080 半解耦 ...

  10. 如何画好ER图

    快速阅读 了解ER图的基本组成,以及如何在viso中画ER图. 什么是ER图 是实体关系图,用矩形表示实体,用椭圆形表示属性,用棱形表示两实体之间的联系.相互用直接联接起来,是一种数据建模工具.用来描 ...