Spring bean的作用域

The scope of this bean: typically "singleton" (one shared instance,
which will be returned by all calls to getBean with the given id), or
"prototype" (independent instance resulting from each call to getBean).

By default, a bean will be a singleton, unless the bean has a parent
bean definition in which case it will inherit the parent's scope.

Singletons are most commonly used, and are ideal for multi-threaded
service objects. Further scopes, such as "request" or "session", might
be supported by extended bean factories (e.g. in a web environment).

在spring2.0之前bean只有2种作用域即:singleton(单实例)、prototype(原型), Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。因此,默认情况下Spring2.0现在有五种类型的Bean。当然,Spring2.0对Bean的类型的设计进行了重构,并设计出灵活的Bean类型支持,理论上可以有无数多种类型的Bean,用户可以根据自己的需要,增加新的Bean类型,满足实际应用需求。

1、singleton作用域

当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例,这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的,单例设计模式表示一个ClassLoader中只有一个class存在,而这里的singleton则表示一个容器对应一个bean,也就是说当一个bean被标识为singleton时候,spring的IOC容器中只会存在一个该bean

2、prototype作用域

prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当与一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)

测试:

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
id="car" class="com.xiya.spring.beans.scope.Car" scope="prototype">
        name="brand" value="BMW"/>
        name="price" value="1000000"/>
package com.xiya.spring.beans.scope;
/**
 * Created by N3verL4nd on 2017/3/20.

 */
public class Car {
    private String brand;
    private int price;
    public Car() {
        System.out.println("Car's constructor");
}

    public Car(String brand, int price) {
        this.brand = brand;
        this.price = price;
}

    public String getBrand() {
        return brand;
}

    public void setBrand(String brand) {
        this.brand = brand;
}

    public int getPrice() {
        return price;
}

    public void setPrice(int price) {
        this.price = price;
}

    @Override
public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
}
}
package com.xiya.spring.beans.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by N3verL4nd on 2017/3/22.

 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-scope.xml");
Car car1 = (Car) context.getBean("car");
Car car2 = (Car) context.getBean("car");
System.out.println(car1 == car2);
}
}

Original: https://www.cnblogs.com/lgh1992314/p/6616204.html
Author: N3verL4nd
Title: Spring bean的作用域

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/540337/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Spring Cloud Gateway 网关内置API

    1 时刻与技术进步,每天一点滴,日久一大步!!! 本博客只为记录,用于学习,如有冒犯,请私信于我。 Original: https://www.cnblogs.com/myitne…

    Java 2023年5月30日
    048
  • 使用Redis+SpringBoot实现定时任务测试

    Redis实现定时任务是基于对RedisKey值的监控 具体代码实现: 建一个SpringBoot项目 引入依赖 "1.0" encoding="UT…

    Java 2023年6月13日
    072
  • JDK成长记8:HashMap的兄弟姐妹们

    LinkedHashMap的源码底层原理 LinkedHashMap继承自HashMap,但是它的底层增加了一个链表来维护 插入或者访问顺序,使得LinkedHashMap变动有顺…

    Java 2023年6月5日
    085
  • java中的集合

    数组的缺点引出集合的好处 数组有很多不足的地方 长度从一开始就必须指定的大小 元素的类型必须一致 使用数组的增删改查,代码比价多比较麻烦 集合的好处 可以动态的保存任意对象 提供了…

    Java 2023年6月6日
    076
  • 自定义MyBatis

    一. 原生 JDBC 1. 原生JDBC使用 public static void main(String[] args) { Connection conn = null; Pr…

    Java 2023年6月5日
    095
  • vue找不到页面自定义404页面

    在vue项目中,如果不做路由处理的话,用户可以直接在url随意输入跳转页面, 默认的时候我们是并没有什么处理。 现在需要做一个自定义的404找不到页面的处理方式。 1.在route…

    Java 2023年6月14日
    070
  • 函数式编程/lambda表达式入门

    函数式编程/lambda表达式入门 本篇主要讲解 lambda表达式的入门,涉及为什么使用函数式编程,以及jdk8提供的函数式接口 和 接口的默认方法 等等 1.什么是命令式编程 …

    Java 2023年6月9日
    094
  • Ceph集群搭建记录

    环境准备 基础环境 node00 192.168.247.144 node00 node01 192.168.247.135 node01 node02 192.168.247.1…

    Java 2023年6月10日
    0102
  • 实时折射、镜面反射shader

    原文链接:http://www.ceeger.com/forum/read.php?tid=3162&fid=2 Unity没有原生的实时镜面反射Shader,分享几个自己…

    Java 2023年5月30日
    079
  • MyBatis 简介及简单使用

    MyBatis 简介1、MyBatis 历史MyBatis最初是Apache的一个开源项目 iBatis, 2010年6月这个项目由Apache Software Foundati…

    Java 2023年6月7日
    071
  • 20220723-Mac上使用IntelliJ IDEA

    IDEA快捷键 IDEA模板 常用模板快捷键 个人随笔 软件:IntelliJ IDEA电脑:Mac IDEA快捷键 打开/关闭 项目视图 快捷键:⌘ + 1 运行项目 快捷键:⌃…

    Java 2023年6月15日
    064
  • 【转】【数学】矩阵求逆的几何意义

    向量:[a1, a2, a3, …, an]矩阵:a11, a12, a13, …, a1na21, a22, a23, …, a2n&#823…

    Java 2023年5月29日
    0197
  • 已数组作为参考过滤数组数据

    this.purchaseDetailList = this.purchaseDetailList.filter((item) => !this.addDataList.so…

    Java 2023年6月5日
    080
  • Spring Boot 实现 RabbitMQ 延迟消费和延迟重试队列

    本文主要摘录自:详细介绍Spring Boot + RabbitMQ实现延迟队列 并增加了自己的一些理解,记录下来,以便日后查阅。 项目源码: spring-boot-rabbit…

    Java 2023年5月30日
    0138
  • 服务器无外网且无yum源的软件安装方式

    无外网部署,无本地yum源部署方法 V_1.0 Author:王欢 Date:2021-06-23 1.查询目标服务器的操作系统版本 2.创建对应版本的最小虚拟机 3.在虚拟机上使…

    Java 2023年6月9日
    077
  • Spring5框架新功能

    一、 1、整个Spring5框架基于java8,运行时兼容java9,许多不建议使用的方法在代码库中被删除 2、Spring5自带通用的日志封装 (1)Spring5已经移除了lo…

    Java 2023年6月7日
    087
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球