Spring Bean的scope

scope 描述的是spring容器如何新建Bean实例的。Spring的Scope有以下的几种Scope注解的实现:

  1. Singleton:一个Spring容器中只有一个Bean实例,此为Spring的默认配置
  2. Prototype:每次调用,创建一个新的实例
  3. Request: 在web项目中,给每一个http request 新建一个bean实例
  4. Session: 在web项目中,给每一个http session新建一个实例
  5. GlobalSession: 这个只是在portal应用中有用,给每一个global http session 新建一个实例

示例演示

(1)编写Singleton的bean

package chapter2;

import org.springframework.stereotype.Service;

/**
 * Created by zhanghongbin01 on 2017/4/10.
 */
@Service
public class DemoSingleService {
}

(2)编写Prototype的Bean

package chapter2;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * Created by zhanghongbin01 on 2017/4/10.
 */
@Service
@Scope("prototype")
public class DemoProtypeService {
}

(3)编写配置类

package chapter2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zhanghongbin01 on 2017/4/10.
 */
@Configuration
@ComponentScan("chapter2")
public class DiConfig {
}

(4)在主函数中启动,进行测试

package chapter2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by zhanghongbin01 on 2017/4/10.
 */
public class TestBeanScopMain {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DiConfig.class);
        DemoSingleService single1 = ctx.getBean(DemoSingleService.class);
        DemoSingleService single2 = ctx.getBean(DemoSingleService.class);

        DemoProtypeService p1 = ctx.getBean(DemoProtypeService.class);
        DemoProtypeService p2 = ctx.getBean(DemoProtypeService.class);

        //single1 和single2默认使用的是单例模式,在整个的系统中保持的是单例
        System.out.println("s1和s2是否相等:"+single1.equals(single2));
        //p1和p2使用的是非单例模式,每次都会创建一个新的实例
        System.out.println("p1和p2是否相等:"+p1.equals(p2));
    }
}

结果演示

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值