【Spring】Spring常用配置-Bean的Scope

简介: 【Spring】Spring常用配置-Bean的Scope

分析

Scope(范围)描述的是Spring容器如何新建Bean的实例的。可以简单的理解成Bean的作用范围!

Spring的Scope有以下的几种,可以通过@Scope注解来实现。

1、singleton:一个Spring容器中只有一个Bean的实例。
    这是Spring的默认配置,也就是不写@Scope("singleton"),全容器共享一个实例。
2、prototype:每次调用都会新建一个Bean的实例。
3、request:Web项目中,给每一个http request新建一个Bean实例。
    也就是每一个request请求,都会新建一个Bean。
4、session:Web项目中,给每一个http session新建一个Bean实例。
    也就是同一个session访问的请求,都是同一个Bean。
5、globalSession:这个只在portal应用中有用,给每一个global http session新建一个Bean实例。


在Spring Batch中还有一个Scope是使用@StepScope的,这里就不介绍了。以后会有博客提到。

现在要去了解的,请自行谷歌。


下面的实例是简单的演示默认的singleton和prototype,分别从Spring容器中获得2次Bean,分别用==与equals判断Bean的实例是否相等!


示例


singleton的Bean

package cn.hncu.p2_1_1Scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/11.
 * Time: 上午 11:09.
 * Explain:Singleton---默认Spring-Scope
 */
@Service//默认@Scope为Singleton-相当于添加
//@Scope("singleton")
public class DemoSingletonService {
}


prototype的Bean

package cn.hncu.p2_1_1Scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/11.
 * Time: 上午 11:23.
 * Explain:编写Prototype的Bean
 */
@Service
@Scope("prototype")
public class DemoPrototypeService {
}


配置类

package cn.hncu.p2_1_1Scope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/11.
 * Time: 上午 11:42.
 * Explain:配置类
 */
@Configuration
@ComponentScan("cn.hncu.p2_1_1Scope.")
public class ScopeConfig {
}


运行

package cn.hncu.p2_1_1Scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * Created with IntelliJ IDEA.
 * User: 陈浩翔.
 * Date: 2016/11/11.
 * Time: 上午 11:43.
 * Explain:运行类
 */
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
        DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
        DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
        DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
        DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
        System.out.println("p1.equals(p2):"+p1.equals(p2));//false
        System.out.println("p1==p2:"+(p1==p2));//false
        System.out.println("s1.equals(s2):"+s1.equals(s2));//true
        System.out.println("s1==s2:"+(s1==s2));//true
    }
}


运行结果

image.png

项目链接—具体包:

https://github.com/chenhaoxiang/Java/tree/master/springBoot/src/main/java/cn/hncu/p2_1_1Scope

目录
相关文章
|
16天前
|
存储 Java 数据安全/隐私保护
|
1天前
|
消息中间件 开发框架 Java
什么是Spring Boot 自动配置?
Spring Boot 是一个流行的 Java 开发框架,它提供了许多便利的功能和工具,帮助开发者快速构建应用程序。其中一个最引人注目的特性是其强大的自动配置功能。
6 0
|
2天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
10 1
|
3天前
|
Java Spring
Spring文件配置以及获取
Spring文件配置以及获取
11 0
|
10天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
21 2
|
12天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
20 1
|
13天前
|
Java 开发者 Spring
Spring Boot中的资源文件属性配置
【4月更文挑战第28天】在Spring Boot应用程序中,配置文件是管理应用程序行为的重要组成部分。资源文件属性配置允许开发者在不重新编译代码的情况下,对应用程序进行灵活地配置和调整。本篇博客将介绍Spring Boot中资源文件属性配置的基本概念,并通过实际示例展示如何利用这一功能。
23 1
|
14天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
32 2
|
17天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
17天前
|
Java Spring 容器
如何用基于 Java 配置的方式配置 Spring?
如何用基于 Java 配置的方式配置 Spring?