Spring Boot 2.x实战5 - Spring 5.x基础3 - Bean的Scope

2.5 Bean的Scope

容器中的Bean的Scope指的是Bean的实例在容器中创建的方式。在容器中默认是singleton,整个容器中只创建一个Bean的实例。常用的还有prototype,每次请求Bean时都会创建一个实例。我们可以通过@Scope注解来设置Scope。

下面两种方式是相同的:

@Service
public class ScopeService {
}
@Service
@Scope(BeanDefinition.SCOPE_SINGLETON) //与@Scope("singleton")相同
public class ScopeService {
}

我们使用@Scope(BeanDefinition.SCOPE_PROTOTYPE)来指定Scope为prototype

@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE) //与@Scope("prototype")相同
public class ScopeService2 {
}

我们除了可以在方法上注解@Scope,也可以在@Bean的类上注解@Scope,如我们对类:

public class ScopeService3 {
}

JavaConfig中配置:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) //与@Scope("prototype")相同
public ScopeService3 scopeService3(){
    return new ScopeService3();
}

这时我们在ScopeInjectService Bean中分别给上面三个Bean注入两次,判断相同类型的两个注入是否相等:

@Service
public class ScopeInjectService {
    private ScopeService scopeService;
    private ScopeService scopeService1;
    private ScopeService2 scopeService2;
    private ScopeService2 scopeService21;
    private ScopeService3 scopeService3;
    private ScopeService3 scopeService31;
	//只有一个构造器,此处可省略@Autowired
    public ScopeInjectService(ScopeService scopeService,
                              ScopeService scopeService1,
                              ScopeService2 scopeService2,
                              ScopeService2 scopeService21,
                              ScopeService3 scopeService3,
                              ScopeService3 scopeService31) {
        this.scopeService = scopeService;
        this.scopeService1 = scopeService1;
        this.scopeService2 = scopeService2;
        this.scopeService21 = scopeService21;
        this.scopeService3 = scopeService3;
        this.scopeService31 = scopeService31;
    }

    public void validateScope(){
        System.out.println(scopeService.equals(scopeService1));//容器内只有一个实例,所以相等
        System.out.println(scopeService2.equals(scopeService21));//请求注入创建新的实例,不相等
        System.out.println(scopeService3.equals(scopeService31));//请求注入创建新的实例,不相等
    }
}

JavaConfig执行:

@Bean
CommandLineRunner scopeClr(ScopeInjectService scopeInjectService){
    return args -> {
      scopeInjectService.validateScope();
    };
}

输出结果是:
在这里插入图片描述

新书推荐:

我的新书《从企业级开发到云原生微服务:Spring Boot 实战》已出版,内容涵盖了丰富Spring Boot开发的相关知识
购买地址:https://item.jd.com/12760084.html

在这里插入图片描述

主要包含目录有:

第一章 初识Spring Boot(快速领略Spring Boot的美丽)
第二章 开发必备工具(对常用开发工具进行介绍:包含IntelliJ IDEA、Gradle、Lombok、Docker等)
第三章 函数式编程
第四章 Spring 5.x基础(以Spring 5.2.x为基础)
第五章 深入Spring Boot(以Spring Boot 2.2.x为基础)
第六章 Spring Web MVC
第七章 数据访问(包含Spring Data JPA、Spring Data Elasticsearch和数据缓存)
第八章 安全控制(包含Spring Security和OAuth2)
第九章 响应式编程(包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC、Reactive Spring Security)
第十章 事件驱动(包含JMS、RabbitMQ、Kafka、Websocket、RSocket)
第11章 系统集成和屁股里(包含Spring Integration和Spring Batch)
第12章 Spring Cloud与微服务
第13章 Kubernetes与微服务(包含Kubernetes、Helm、Jenkins、Istio)
多谢大家支持。

发布了116 篇原创文章 · 获赞 10 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/wiselyman/article/details/105732625