在您的应用程序中,由 Spring IoC 容器管理的形成其核心的对象被称为 "bean"。一个 bean 是由 Spring IoC 容器实例化、组装和管理的对象

这些 bean 是通过您提供给容器的配置元数据创建的。Bean 定义包含了所谓的配置元数据,容器需要了解以下内容:

  • 如何创建一个 bean
  • Bean 的生命周期详细信息
  • Bean 的依赖关系

上述所有的配置元数据都转化为每个 bean 定义的以下属性集合。

序号 属性和描述
1 class
这是必填属性,指定要用于创建 beanbean 类。
2 name
此属性唯一地指定 bean 标识符。在基于 XML 的配置元数据中,您可以使用 id 和/或 name 属性来指定 bean 标识符。
3 scope
此属性指定从特定 bean 定义创建的对象的范围
4 constructor-arg
这用于注入依赖项
5 properties
这用于注入依赖项
6 autowiring mode
这用于注入依赖项
7 lazy-initialization mode
延迟初始化的 bean 告诉 IoC 容器在首次请求时创建 bean 实例,而不是在启动时创建。
8 initialization method
在容器设置了 bean 的所有必需属性之后,要调用的回调函数
9 destruction method
在包含 bean 的容器销毁时要使用的回调函数

Spring 配置元数据

Spring IoC 容器与实际编写配置元数据的格式完全解耦。以下是向 Spring 容器提供配置元数据的三种重要方法:

  • 基于 XML 的配置文件。
  • 基于注解的配置。
  • 基于 Java 的配置。

您已经看到了如何将基于 XML 的配置元数据提供给容器,但让我们看一下包含不同 bean 定义的 XML 配置文件的另一个示例,包括延迟初始化、初始化方法和销毁方法。

  1. <?xml version = "1.0" encoding = "UTF-8"?>
  2. <beans xmlns = "http://www.springframework.org/schema/beans"
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation = "http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- 一个简单的 `bean` 定义 -->
  7. <bean id = "..." class = "...">
  8. <!-- 此处是该 `bean` 的协作者和配置 -->
  9. </bean>
  10. <!-- 启用延迟初始化的 `bean` 定义 -->
  11. <bean id = "..." class = "..." lazy-init = "true">
  12. <!-- 此处是该 `bean` 的协作者和配置 -->
  13. </bean>
  14. <!-- 具有初始化方法的 `bean` 定义 -->
  15. <bean id = "..." class = "..." init-method = "...">
  16. <!-- 此处是该 `bean` 的协作者和配置 -->
  17. </bean>
  18. <!-- 具有销毁方法的 `bean` 定义 -->
  19. <bean id = "..." class = "..." destroy-method = "...">
  20. <!-- 此处是该 `bean` 的协作者和配置 -->
  21. </bean>
  22. <!-- 更多的 `bean` 定义在此处 -->

Spring 中的 Bean 作用域

在定义 <bean> 时,您可以选择为该 bean 声明一个作用域。例如,要强制 Spring 每次需要时生成新的 bean 实例,您应该将 bean 的作用域属性声明为 prototype。类似地,如果您希望 Spring 每次需要时返回相同的 bean 实例,您应该将 bean 的作用域属性声明为 singleton

Spring 框架支持以下五种作用域,其中三种仅在使用与 Web 相关的 ApplicationContext 时才可用。

序号 作用域 & 描述
1 singleton
bean 定义的作用域限制为 Spring IoC 容器中的单个实例(默认)。
2 prototype
将单个 bean 定义的作用域限制为具有任意数量的对象实例。
3 request
bean 定义的作用域限制为 HTTP 请求。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。
4 session
bean 定义的作用域限制为 HTTP 会话。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。
5 global-session
bean 定义的作用域限制为全局 HTTP 会话。仅在具有与 Web 相关的 Spring ApplicationContext 的情况下有效。

当讨论与

Web 相关的 Spring ApplicationContext 时,将讨论其他三种作用域。

单例作用域(singleton

如果将作用域设置为 singletonSpring IoC 容器将创建一个对象的确切实例,该实例由 bean 定义定义。此单个实例存储在此类单例 bean 的缓存中,对于该命名 bean 的所有后续请求和引用都会返回缓存的对象。

默认作用域始终是 singleton。但是,当您需要一个且仅一个 bean 实例时,您可以在 bean 配置文件中将作用域属性设置为 singleton,如下所示:

  1. <!-- 具有 `singleton` 作用域的 `bean` 定义 -->
  2. <bean id="..." class="..." scope="singleton">
  3. <!-- 此处放置此 `bean` 的协作者和配置 -->

示例

假设您已经准备好 Eclipse IDE,并采取以下步骤创建 Spring 应用程序:

步骤

  1. 创建一个名为 SpringExample 的项目,在创建的项目中的 src 文件夹下创建一个名为 com.tutorialspoint 的包
  2. 使用"Add External JARs"选项添加所需的 Spring
  3. com.tutorialspoint 包下创建 JavaHelloWorldMainApp
  4. src 文件夹下创建 Beans 配置文件 Beans.xml
  5. 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并按以下说明运行应用程序。

以下是 HelloWorld.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class HelloWorld {
  3. private String message;
  4. public void setMessage(String message){
  5. this.message = message;
  6. }
  7. public void getMessage(){
  8. System.out.println("Your Message : " + message);
  9. }
  10. }

以下是 MainApp.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  7. HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
  8. objA.setMessage("I'm object A");
  9. objA.getMessage();
  10. HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
  11. objB.getMessage();
  12. }
  13. }

以下是 singleton 作用域所需的 Beans.xml 配置文件的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="singleton">
  7. </bean>
  8. </beans>

当您完成创建源代码和 bean 配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:

  1. Your Message : I'm object A
  2. Your Message : I'm object A

原型作用域(prototype

如果将作用域设置为 prototypeSpring IoC 容器将在每次请求特定 bean 时创建该对象的新 bean 实例。通常,对于所有有状态的 bean,使用 prototype 作用域,对于无状态的 bean,使用 singleton 作用域。

要定义原型作用域,您可以在 bean 配置文件中将作用域属性设置为 prototype,如下所示:

  1. <!-- 具有 `prototype` 作用域的 `bean` 定义 -->
  2. <bean id="..." class="..." scope="prototype">
  3. <!-- 此处放置此 `bean` 的协作者和配置 -->
  4. </bean>

示例

假设您已经准备好 Eclipse IDE,并采取以下步骤创建 Spring 应用程序:

步骤

  1. 创建一个名为 SpringExample 的项目,在创建的项目中的 src 文件夹下创建一个名为 com.tutorialspoint 的包
  2. 使用"Add External JARs"选项添加所需的 Spring
  3. com.tutorialspoint 包下创建 JavaHelloWorldMainApp
  4. src 文件夹下创建 Beans 配置文件 Beans.xml
  5. 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并按以下说明运行应用程序。

以下是 HelloWorld.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class HelloWorld {
  3. private String message;
  4. public void setMessage(String message){
  5. this.message = message;
  6. }
  7. public void getMessage(){
  8. System.out.println("Your Message : " + message);
  9. }
  10. }

以下是 MainApp.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  7. HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
  8. objA.setMessage("I'm object A");
  9. objA.getMessage();
  10. HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
  11. objB.getMessage();
  12. }
  13. }

以下是 prototype 作用域所需的 Beans.xml 配置文件的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="prototype">
  7. </bean>
  8. </beans>

当您完成创建源代码和bean配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:

  1. Your Message : I'm object A
  2. Your Message : I'm object A

最后

为了方便其他设备和平台的小伙伴观看往期文章:

微信公众号搜索:Let us Coding,关注后即可获取最新文章推送

看完如果觉得有帮助,欢迎 点赞、收藏、关注

掌握 Spring IoC 容器与 Bean 作用域:详解 singleton 与 prototype 的使用与配置的更多相关文章

  1. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  2. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  3. Spring IOC容器创建bean过程浅析

    1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...

  4. Spring IoC 容器和 bean 对象

    程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...

  5. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  6. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  7. Spring IOC容器对bean的生命周期进行管理的过程

    1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...

  8. Spring基础——在 IOC 容器中 Bean 之间的关系

    一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...

  9. Spring IOC 容器源码分析 - 获取单例 bean

    1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...

  10. Spring(十二):IOC容器中Bean的生命周期方法

    IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...

随机推荐

  1. ASP.NET 通过拦截器记录错误日志

    前言 主要是记录一下实现的错误日志拦截,可以在拦截器里面控制返回的信息,把错误信息处理后返回给请求端. 代码实战 拦截器 /// <summary> /// 接口异常捕捉过滤器 /// & ...

  2. 【LeetCode贪心#05】K 次取反后最大化的数组和(自定义sort、二重贪心)

    K次取反后最大化的数组和 力扣题目链接(opens new window) 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这 ...

  3. 矩池云产品最新动态 All in One

    AI/ML 的不断革新,让我们看到了更多激动人心的应用方向,也迸发了更多的训练&应用场景. 在用户的反馈和建议下,矩池云持续丰富和优化在 AI+Science 链路上的相关产品,为了帮助研究人 ...

  4. 【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden

    问题描述 在Azure Data Factory (数据工厂)中,调用同在Azure中的Function App函数,却出现403 - Forbidden错误. 截图如下: 问题解答 访问Azure ...

  5. 【Azure 应用服务】Function App / App Service 连接 Blob 报错

    问题描述 因 Blob 启用了防火墙功能,但是当把App Service 或 Function App的出站IP地址都加入到Blob的白名单中,为什么访问还是403错误呢? 问题解答 Azure St ...

  6. Jmeter 如何连接mysql数据库?

    1 首先安装jmeter jdbc 插件 JDBC驱动包下载教程:https://blog.csdn.net/qq_50896685/article/details/129154801 2 安装好后将 ...

  7. Delete `␍`

    新电脑遇到的问题 Delete `␍`eslint(prettier/prettier) 网上一搜,一堆解决办法,没有一个说到点子上,都是表面上如何避免,如何设置VSCODE... 都知道是换行符的问 ...

  8. 那些.NET中的连接池

    前言 在.NET中,连接池被广泛用于管理和优化不同类型资源的连接.连接池可以减少建立和关闭连接所需的时间和资源消耗,从而提高了应用程序的性能和响应能力. HttpClient中的连接池 System. ...

  9. [.Net]使用Soa库+Abp搭建微服务项目框架(四):动态代理和RPC

    ​上一章我们完成了小项目的面向服务体系改造,你或许一直在思考一个问题.为什么要将业务独立成微服务? 微服务原理 以一个健康医疗系统为例, 这个系统包含了用户模块,问卷的发放与填写,图表显示,报表生成与 ...

  10. Nginx安装nginx-rtmp-module模块

    简介 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展. nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最 ...