Spring bean定义Spring Bean 的作用域

Spring bean定义

目录

Spring bean定义

 

Spring配置元数据

Spring Bean 的作用域

 

singleton作用域:

 

原型作用域:

示例:


 

形成应用程序的骨干是由Spring IoC容器所管理的对象称为bean。bean被实例化,组装,并通过Spring IoC容器所管理的对象。这些bean由容器提供,例如,在XML的<bean/>定义,已经看到了前几章的形式配置元数据创建。

bean定义包含所需要的容器要知道以下称为配置元数据的信息:

  • 如何创建一个bean

  • Bean 生命周期的详细信息

  • Bean 依赖关系

上述所有配置元数据转换成一组的下列属性构成每个bean的定义。

属性描述
class此属性是强制性的,并指定bean类被用来创建bean。
name此属性指定唯一bean标识符。在基于XML的配置元数据时,您可以使用id和/或name属性来指定bean标识符
scope该属性指定一个特定的bean定义创建,它会在bean作用域本章要讨论的对象范围。
constructor-arg这是用来注入的依赖关系,并在接下来的章节中进行讨论。
properties这是用来注入的依赖关系,并在接下来的章节中进行讨论。
autowiring mode这是用来注入的依赖关系,并在接下来的章节中进行讨论。
lazy-initialization mode延迟初始化的bean告诉IoC容器创建bean实例时,它首先要求,而不是在启动时。
initialization method回调只是在bean的所有必要属性后调用已设置的容器。它会在bean的生命周期章节中讨论。
destruction method当包含该bean容器被销毁所使用的回调。它会在bean的生命周期章节中讨论。

 

Spring配置元数据

Spring IoC容器完全由在此配置元数据实际写入的格式解耦。有下列提供的配置元数据的Spring容器三个重要的方法:

  1. 基于XML的配置文件。

  2. 基于注解的配置

  3. 基于Java的配置

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

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- A simple bean definition --><bean id="..." class="..."><!-- collaborators and configuration for this bean go here --></bean><!-- A bean definition with lazy init set on --><bean id="..." class="..." lazy-init="true"><!-- collaborators and configuration for this bean go here --></bean><!-- A bean definition with initialization method --><bean id="..." class="..." init-method="..."><!-- collaborators and configuration for this bean go here --></bean><!-- A bean definition with destruction method --><bean id="..." class="..." destroy-method="..."><!-- collaborators and configuration for this bean go here --></bean><!-- more bean definitions go here --></beans>

 

有关基于注解的配置在一个单独的章节讨论。在一个单独的章节刻意保留它,因为希望能掌握一些Spring其他的重要概念,在开始用注解依赖注入来编程。

Spring Bean 的作用域

 

 

 

当定义一个Spring的<bean>,必须声明bean 作用域的选项。例如,要强制Spring需要产生一个新的bean实例,应该声明bean的scope属性为prototype。如果你希望Spring 每次都返回同一个bean实例,应该声明bean的作用域,方式类似属性是单例。

Spring框架支持以下五个作用域,其中三个只有当您使用Web感知的 ApplicationContext 可用。

范围描述
singletonThis scopes the bean definition to a single instance per Spring IoC container (default).
prototypeThis scopes a single bean definition to have any number of object instances.
requestThis scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
sessionThis scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-sessionThis scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

本章将讨论前两个范围和其余三将讨论的时候,我们将讨论有关Web感知Spring的ApplicationContext。

 

singleton作用域:

如果范围设置为单例,Spring IoC容器创建了一个由该bean定义的对象只有一个实例。这个单一实例存储在这样的单例bean的高速缓存,以及所有后续请求和引用针对该bean返回缓存对象。

默认范围是始终单例,但是当你需要bean的一个实例,可以设置的范围属性单例在bean配置文件中,如下图所示:

 

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton"><!-- collaborators and configuration for this bean go here -->
</bean>

示例:

让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤描述
1Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3Create Java classes HelloWorld and MainApp under the com.manongjc package.
4Create Beans configuration file Beans.xml under the src folder.
5The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是HelloWorld.java 文件的内容:

package com.manongjc;public class HelloWorld {private String message;public void setMessage(String message){this.message  = message;}public void getMessage(){System.out.println("Your Message : " + message);}
}

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

​package com.manongjc;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");HelloWorld objA = (HelloWorld) context.getBean("helloWorld");objA.setMessage("I'm object A");objA.getMessage();HelloWorld objB = (HelloWorld) context.getBean("helloWorld");objB.getMessage();}
}​

以下是需要singleton作用域配置文件beans.xml文件:

​<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="helloWorld" class="com.manongjc.HelloWorld" scope="singleton"></bean></beans>​

一旦创建源代码和bean配置文件来完成,运行应用程序。如果一切顺利,这将打印以下信息:

​Your Message : I'm object A
Your Message : I'm object A​

原型作用域:

如果范围设置为原型,那么Spring IoC容器创建对象的新的bean实例为每个特定的bean发出请求时的时间。作为一项规则,使用prototype作用域为所有状态的bean类和singleton作用域为无状态的bean。

要定义一个原型作用域,可以设置的范围属性为原型的bean配置文件中,如下图所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype"><!-- collaborators and configuration for this bean go here -->
</bean>

示例:

让我们在地方工作的Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤描述
1Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3Create Java classes HelloWorld and MainApp under the com.manongjc package.
4Create Beans configuration file Beans.xml under the src folder.
5The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是HelloWorld.java 文件的内容:

​package com.manongjc;public class HelloWorld {private String message;public void setMessage(String message){this.message  = message;}public void getMessage(){System.out.println("Your Message : " + message);}
}​

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

​package com.manongjc;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");HelloWorld objA = (HelloWorld) context.getBean("helloWorld");objA.setMessage("I'm object A");objA.getMessage();HelloWorld objB = (HelloWorld) context.getBean("helloWorld");objB.getMessage();}
}​

以下是必需的原型作用域的配置文件beans.xml:

​<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="helloWorld" class="com.manongjc.HelloWorld" scope="prototype"></bean></beans>​

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:

​Your Message : I'm object A
Your Message : null​

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/149017.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

mysql双主+双从集群连接模式

架构图&#xff1a; 详细内容参考&#xff1a; 结果展示&#xff1a; 178.119.30.14(主) 178.119.30.15(主) 178.119.30.16(从) 178.119.30.17(从)

添加驱动模块到内核的两种方法

添加驱动模块到内核的两种方法 1. 放在内核源代码树中 步骤总结&#xff1a; 新建文件夹编写Makefile、编写Kconfig修改上层Kconfig执行make menuconfig执行make zImage 或 make modules 1.1 源码放入文件夹 例如&#xff1a;添加一个按键字符设备模块 在内核目录下的 dri…

【推荐系统】多任务学习模型

介绍一些多任务学习模型了解是如何处理多任务分支的。 ESSM, Entire Space Multi-Task Model 阿里提出的ESSM全称Entire Space Multi-Task Model&#xff0c;全样本空间的多任务模型&#xff0c;有效地解决了CVR建模&#xff08;转化率预估&#xff09;中存在的两个非常重要…

MATLAB中plot3函数用法

目录 语法 说明 向量和矩阵数据 表数据 其他选项 示例 绘制三维螺旋图 绘制多个线条 使用矩阵绘制多个线条 指定等间距刻度单位和轴标签 将点绘制为不带线的标记 自定义颜色和标记 指定线型 在绘图后修改线条 绘制表中的数据 在 x 和 y 轴上绘制多个表变量 指…

10.2 调试事件获取DLL装载

理解了如何通过调试事件输出当前进程中寄存器信息&#xff0c;那么实现加载DLL模块也会变得很容易实现&#xff0c;加载DLL模块主要使用LOAD_DLL_DEBUG_EVENT这个通知事件&#xff0c;该事件可检测进程加载的模块信息&#xff0c;一旦有新模块被加载或装入那么则会触发一个通知…

Qt扫盲-QSqlTableModel理论总结

QSqlTableModel理论总结 一、概述二、使用1. 与 view 视图绑定2. 做中间层&#xff0c;不显示 三、常用函数 一、概述 QSqlTableModel是用于从单个表读写数据库记录的高级接口。它构建在较低级的QSqlQuery之上&#xff0c;可用于向QTableView 等视图类提供数据。这个主要是对单…

苹果V3签名是什么?优势是什么?什么场合需要应用到?该怎么部署?

v3签名&#xff0c;或称为Apple Developer Program v3签名&#xff0c;是苹果公司在2021年6月推出的一种签名格式&#xff0c;用于对应用程序进行签名和验证。 它是取代了之前的v2签名格式&#xff0c;用于增强应用程序的安全性和完整性。 v3签名能够做到以下几点&#xff1a;…

【Linux基础】Linux发展史

&#x1f449;系列专栏&#xff1a;【Linux基础】 &#x1f648;个人主页&#xff1a;sunny-ll 一、前言 本篇主要介绍Linux的发展历史&#xff0c;这里并不需要我们掌握&#xff0c;但是作为一个合格的Linux学习者与操作者&#xff0c;这些东西是需要了解的&#xff0c;而且…

前端JavaScript入门到精通,javascript核心进阶ES6语法、API、js高级等基础知识和实战 —— Web APIs(五)

思维导图 Bom操作 一、Window对象 1.1 BOM(浏览器对象模型) <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"vi…

k8s集群-6(daemonset job cronjob控制器)

Daemonset 一个节点部署一个节点 当有节点DaemonSet 确保全部 (或者某些) 节点上运行一个 Pod 的副本。加入集群时&#xff0c;也会为他们新增一个 Pod 。当有节点从集群移除时&#xff0c;这些Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。 DaemonSet 的典型用…

Boost程序库完全开发指南:1.1-C++基础知识点梳理

主要整理了N多年前&#xff08;2010年&#xff09;学习C的时候开始总结的知识点&#xff0c;好长时间不写C代码了&#xff0c;现在LLM量化和推理需要重新学习C编程&#xff0c;看来出来混迟早要还的。 1.shared_ptr 解析&#xff1a;shared_ptr是一种计数指针&#xff0c;当引…

数字电路逻辑与设计 之循环码和 移存码

有发现错误的能力&#xff0c;不能纠正 只能检查单次的错误&#xff0c;不能完全抗干扰 可以按照上面的方法来循环构造 移存码可以通过前推后推来实现

pycharm配置python3.8版本专门用于undecteded_chromedriver测试

pycharm配置python3.8版本专门用于undecteded_chromedriver测试 作者&#xff1a;虚坏叔叔 博客&#xff1a;https://pay.xuhss.com 早餐店不会开到晚上&#xff0c;想吃的人早就来了&#xff01;&#x1f604; 一、Pycharm及python环境的配置 1.安装python-3.8.7rc1-amd64.e…

MySQL进阶_3.性能分析工具的使用

文章目录 第一节、数据库服务器的优化步骤第二节、查看系统性能参数第三节、 慢查询日志第四节、 查看 SQL 执行成本第五节、 分析查询语句&#xff1a;EXPLAIN5.1 基本语法5.2 EXPLAIN各列作用 第一节、数据库服务器的优化步骤 当我们遇到数据库调优问题的时候&#xff0c;可…

十三、Django之添加用户(原始方法实现)

修改urls.py path("user/add/", views.user_add),添加user_add.html {% extends layout.html %} {% block content %}<div class"container"><div class"panel panel-default"><div class"panel-heading"><h3 c…

全志ARM926 Melis2.0系统的开发指引①

全志ARM926 Melis2.0系统的开发指引① 1. 编写目的2. Melis2.0 系统概述3. Melis2.0 快速开发3.1. Melis2.0 SDK 目录结构3.2. Melis2.0 编译环境3.3. Melis2.0 固件打包3.4. Melis2.0 固件烧录3.5.串口打印信息3.6. Melis2.0 添加和调用一个模块3.6.1. 为什么划分模块&#xf…

[GWCTF 2019]枯燥的抽奖

参考 https://www.cnblogs.com/AikN/p/15764428.html [GWCTF 2019]枯燥的抽奖-CSDN博客 打开环境 笑死我了&#xff0c;怎么那么像我高中校长 查看源代码 看到check.php&#xff0c;去访问一下 ok看到源代码了 因为上次做过&#xff0c;看到这个我就想到用php_mt_seed逆推…

【Hello Linux】多路转接之 epoll

本篇博客介绍&#xff1a; 多路转接之epoll 多路转接之epoll 初识epollepoll相关系统调用epoll的工作原理epoll服务器编写成员变量构造函数 循环函数HandlerEvent函数epoll的优缺点 我们学习epoll分为四部分 快速理解部分概念 快速的看一下部分接口讲解epoll的工作原理手写epo…

解决ASP.NET Core的中间件无法读取Response.Body的问题

概要 本文主要介绍如何在ASP.NET Core的中间件中&#xff0c;读取Response.Body的方法&#xff0c;以便于我们实现更多的定制化开发。本文介绍的方法适用于.Net 3.1 和 .Net 6。 代码和实现 现象解释 首先我们尝试在自定义中间件中直接读取Response.Body&#xff0c;代码如…

Arcgis提取玉米种植地分布,并以此为掩膜提取遥感影像

Arcgis提取玉米种植地分布上&#xff0c;并以此为掩膜提取遥感影像 一、问题描述 因为之前反演是整个研究区&#xff0c;然而土地利用类型有很多类&#xff0c;只在农田或者植被上进行反演&#xff0c;需要去除水体、建筑等其他类型&#xff0c;如何处理得到下图中只有耕地类…