Bean的scope:

1、Singleton(单例): 一个Spring容器只有以这个Bean实例。

2、prototype(多例): 每次调用新建一个Bean的实例。

3、request:一个http request请求一个Bean实例。

4、Session:一个http session请求一个Bean实例。

5、GlobalSession:portal应用中有用

 1 package com.wisely.heighlight_spring4.ch2.scope;
 2 
 3 import org.springframework.context.annotation.Scope;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 @Scope("singleton")        //配置作用域单列(默认就是单列)
 8 public class DemoSingletonService {
 9 
10 }
 1 package com.wisely.heighlight_spring4.ch2.scope;
 2 
 3 import org.springframework.context.annotation.Scope;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 @Scope("prototype")            //配置作用域是多列
 8 public class DemoPrototypeService {
 9 
10 }
 1 package com.wisely.heighlight_spring4.ch2.scope;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 @Configuration
 7 @ComponentScan("com.wisely.heighlight_spring4.ch2.scope")
 8 public class ScopeConfig {
 9 
10 }
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)));
    }
}

优质内容筛选与推荐>>
1、装饰者模式输入
2、JS获取URL指定的参数值
3、【POJ 1094】拓扑排序
4、input 选择 全选 反选 的js代码
5、OA,MOSS平台OA系统,开源OA,OA平台


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn