Spring Bean作用域简介
按照spring官网说明,分成如下五种
Scope | Description |
---|---|
singleton |
(Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype |
Scopes a single bean definition to any number of object instances. |
request |
Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring |
session |
Scopes a single bean definition to the lifecycle of an HTTP |
application |
Scopes a single bean definition to the lifecycle of a |
websocket |
Scopes a single bean definition to the lifecycle of a |
singleton 单例模式 spring默认容器里面的一个对象的实例只有一个
举个栗子:
public class Person {public String getName() {return name;}public void setName(String name) {this.name = name;}private String name;@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}
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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="person" class="demo.guhong.test04.Person" scope="singleton" name="person2"><property name="name" value="bitch"/>
</bean>
</beans>
测试:
public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("testbeans.xml");Person person = (Person) context.getBean("person");Person person2 = (Person) context.getBean("person2");System.out.println(person == person2);}
----------------------------------------------------
true
Process finished with exit code 0
prototype 原型模式:每次从容器中取出来的时候,都是新对象
还是上面的Person,修改xml 中bean属性:scope
<bean id="person" class="demo.guhong.test04.Person" scope="prototype" name="person2"><property name="name" value="bitch"/>
</bean>
结果:
----------------------------------------------------
false
Process finished with exit code 0
request sessoion application websocket
都是在web中用到各自对应不同情形的bean存活时间段,后续有机会待补充
Spring Bean作用域简介相关推荐
- spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...
- Spring Bean作用域实例
在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例 - 每个Spring IoC 容器返回一个bean实例 原型- ...
- spring:Bean作用域
在配置文件中定义Bean时,用户不但可以配置Bean的属性值及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响. spring 4.0中所支持的作用域: ...
- Struts2 Action 与Spring bean 作用域
struts2 的action 是没有scope的,但通过引用spring bean 可以达到有scope功能. <action name="xxxAction" class ...
- spring bean作用域_Srping中Bean的三种装配方式:大魏Java记10
一.Bean的作用域 Spring在初始化一个Bean实例时,可以同时为其指定特定的作用域.作用域将会对Bean的生命周期和创建方式产生影响. Bean的作用域类型: Singleton作用域是Spr ...
- Spring Bean 作用域之间的区别?
Spring 容器中的bean 可以分为5 个范围.所有范围的名称都是自说明的,但是为了避免混淆,还是让我们来解释一下: 1.singleton:这种bean 范围是默认的,这种范围确保不管接受到多少 ...
- Spring Bean作用域与生命周期
目录 Bean的作用域: Bean有六大行为模式 1.singleton:单例模式(默认) 2.prototype: 原型模式(多例模式) 3.request: 请求作用域(Spring MVC) 4 ...
- Spring IOC中 Bean 作用域
Spring Bean 作用域 Spring 3 中为Bean定义了5种作用域,它们是:singleton(单例).prototype(原型).request.session 和 global se ...
- Spring5参考指南:Bean作用域
文章目录 Bean作用域简介 Singleton作用域 Prototype作用域 Singleton Beans 中依赖 Prototype-bean web 作用域 Request scope Se ...
- 【SpringBoot】查看运行环境中所有的spring bean
前言 spring boot : 2.0.0.RELEASE maven eclipse 在开发&调试过程中,提示某个Bean找不到.此时就需要查看运行环境中有没有这个bean,以便快速排除出 ...
最新文章
- java yii_一条路线不起作用(yii)
- SNAT和DNAT的区别
- 音视频技术开发周刊 | 228
- stylus之内置方法(Built-in Functions)
- python 三引号_Python 简明教程 --- 4,Python 变量与基本数据类型
- 飞鸽传书转载:老板的忠告
- 给开源项目贡献代码的经历
- C++函数内部实现的规则
- JQuery小插件,Selected插件1
- python模块分析之time和datetime模块
- 解决Layui表格需表头固定悬浮的问题
- UE4纯蓝图项目接入Steam服务(一)将游戏连接到Steam
- android 脚本运行程序,用android app运行脚本
- 解决IE浏览器jQuery执行ajax不响应问题
- android数据格式化,手机格式化了?教你找回安卓手机误删数据
- 基于python毕业设计毕设课题选题参考
- 51nod 1740蜂巢迷宫
- 操作系统考研复试、工作面试常见问题及答案
- ROS入门-ROS的安装及编写简单的节点talker和listener
- 愿你一生欢喜,不为世俗裹挟 | 笔记摘要
热门文章
- 一些j2ee的视频资料
- 利用Java编写自动关机程序(包括输入、输出、控制电脑自动关机)
- 计算机辅助审计在外汇,外汇管理领域计算机辅助审计
- php jquery alert 美化,jquery插件hiAlert实现网页对话框美化_jquery
- 屏幕录像专家 EXE视频转MP4
- 可能是比原版Firefox更好用的个人定制版Firefox
- python2.0迅雷下载_【Tomato DualWan】迅雷离线下载完美教程
- Mac触控板Magic Trackpad的常用手势
- vue 开发app处理手机返回键问题
- java 实现微信搜索附近人功能