zoukankan      html  css  js  c++  java
  • 03点睛Spring4.1-scope

    转载: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
  • 相关阅读:
    在线用户数与并发用户数的区别和比例关系
    MYSQL中数据类型介绍
    5分钟教你搞定RAID模式
    nginx与fastdfs配置详解与坑
    Fastdfs group通过添加硬盘扩容
    fastDfs配置文件tracker.conf配置详解
    FastDFS配置详解之Storage配置
    mysql修改库名
    vCenter Server Appliance(VCSA )6.7部署指南
    如何知道一个函数的执行时间简单案例
  • 原文地址:https://www.cnblogs.com/Jeely/p/11949794.html
Copyright © 2011-2022 走看看