按照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 ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

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作用域简介相关推荐

  1. spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...

  2. Spring Bean作用域实例

    在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例 - 每个Spring IoC 容器返回一个bean实例 原型- ...

  3. spring:Bean作用域

    在配置文件中定义Bean时,用户不但可以配置Bean的属性值及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响. spring 4.0中所支持的作用域: ...

  4. Struts2 Action 与Spring bean 作用域

    struts2 的action 是没有scope的,但通过引用spring bean 可以达到有scope功能. <action name="xxxAction" class ...

  5. spring bean作用域_Srping中Bean的三种装配方式:大魏Java记10

    一.Bean的作用域 Spring在初始化一个Bean实例时,可以同时为其指定特定的作用域.作用域将会对Bean的生命周期和创建方式产生影响. Bean的作用域类型: Singleton作用域是Spr ...

  6. Spring Bean 作用域之间的区别?

    Spring 容器中的bean 可以分为5 个范围.所有范围的名称都是自说明的,但是为了避免混淆,还是让我们来解释一下: 1.singleton:这种bean 范围是默认的,这种范围确保不管接受到多少 ...

  7. Spring Bean作用域与生命周期

    目录 Bean的作用域: Bean有六大行为模式 1.singleton:单例模式(默认) 2.prototype: 原型模式(多例模式) 3.request: 请求作用域(Spring MVC) 4 ...

  8. ​Spring IOC中 Bean 作用域

    ​Spring Bean 作用域 Spring 3 中为Bean定义了5种作用域,它们是:singleton(单例).prototype(原型).request.session 和 global se ...

  9. Spring5参考指南:Bean作用域

    文章目录 Bean作用域简介 Singleton作用域 Prototype作用域 Singleton Beans 中依赖 Prototype-bean web 作用域 Request scope Se ...

  10. 【SpringBoot】查看运行环境中所有的spring bean

    前言 spring boot : 2.0.0.RELEASE maven eclipse 在开发&调试过程中,提示某个Bean找不到.此时就需要查看运行环境中有没有这个bean,以便快速排除出 ...

最新文章

  1. java yii_一条路线不起作用(yii)
  2. SNAT和DNAT的区别
  3. 音视频技术开发周刊 | 228
  4. stylus之内置方法(Built-in Functions)
  5. python 三引号_Python 简明教程 --- 4,Python 变量与基本数据类型
  6. 飞鸽传书转载:老板的忠告
  7. 给开源项目贡献代码的经历
  8. C++函数内部实现的规则
  9. JQuery小插件,Selected插件1
  10. python模块分析之time和datetime模块
  11. 解决Layui表格需表头固定悬浮的问题
  12. UE4纯蓝图项目接入Steam服务(一)将游戏连接到Steam
  13. android 脚本运行程序,用android app运行脚本
  14. 解决IE浏览器jQuery执行ajax不响应问题
  15. android数据格式化,手机格式化了?教你找回安卓手机误删数据
  16. 基于python毕业设计毕设课题选题参考
  17. 51nod 1740蜂巢迷宫
  18. 操作系统考研复试、工作面试常见问题及答案
  19. ROS入门-ROS的安装及编写简单的节点talker和listener
  20. 愿你一生欢喜,不为世俗裹挟 | 笔记摘要

热门文章

  1. 一些j2ee的视频资料
  2. 利用Java编写自动关机程序(包括输入、输出、控制电脑自动关机)
  3. 计算机辅助审计在外汇,外汇管理领域计算机辅助审计
  4. php jquery alert 美化,jquery插件hiAlert实现网页对话框美化_jquery
  5. 屏幕录像专家 EXE视频转MP4
  6. 可能是比原版Firefox更好用的个人定制版Firefox
  7. python2.0迅雷下载_【Tomato DualWan】迅雷离线下载完美教程
  8. Mac触控板Magic Trackpad的常用手势
  9. vue 开发app处理手机返回键问题
  10. java 实现微信搜索附近人功能