Spring Bean作用域实例
- 单例 - 每个Spring IoC 容器返回一个bean实例
- 原型- 当每次请求时返回一个新的bean实例
- 请求 - 返回每个HTTP请求的一个Bean实例
- 会话 - 返回每个HTTP会话的一个bean实例
- 全局会话- 返回全局HTTP会话的一个bean实例
package com.yiibai.customer.services;public class CustomerService
{String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
<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-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService" /></beans>
执行结果:
package com.yiibai.common;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.customer.services.CustomerService;public class App
{public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});CustomerService custA = (CustomerService)context.getBean("customerService");custA.setMessage("Message by custA");System.out.println("Message : " + custA.getMessage());//retrieve it againCustomerService custB = (CustomerService)context.getBean("customerService");System.out.println("Message : " + custB.getMessage());}
}
输出结果
Message : Message by custA
Message : Message by custA
由于 bean 的 “CustomerService' 是单例作用域,第二个通过提取”custB“将显示消息由 ”custA' 设置,即使它是由一个新的 getBean()方法来提取。在单例中,每个Spring IoC容器只有一个实例,无论多少次调用 getBean()方法获取它,它总是返回同一个实例。
<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-2.5.xsd"><bean id="customerService" class="com.yiibai.customer.services.CustomerService" scope="prototype"/></beans>
运行-执行
Message : Message by custA
Message : null
package com.yiibai.customer.services;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;@Service
@Scope("prototype")
public class CustomerService
{String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.yiibai.customer" /></beans>
Spring Bean作用域实例相关推荐
- spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...
- Spring Bean 作用域之间的区别?
Spring 容器中的bean 可以分为5 个范围.所有范围的名称都是自说明的,但是为了避免混淆,还是让我们来解释一下: 1.singleton:这种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作用域与生命周期
目录 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 ...
- Spring bean相关
Spring中指定Bean的作用于的方式 以下四种为例: 单例(默认,可以不用特殊表明) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) ...
- Spring的作用域与生命周期
文章目录 一.lombok的安装与使用 二.Spring作用域 二.Bean原理分析 执行流程 Bean的生命周期 一.lombok的安装与使用 lombok插件可以提供给我们一些注释,这些注释可以很 ...
最新文章
- c#拼图碎片形状_拼图游戏C#代码
- 12种提升视频质量的方法
- 小米4刷centos_给大家推荐两款小米的产品
- linux 扩展挂载盘大小_Linux 添加挂载硬盘(包含挂载大于2T以上硬盘)
- 那些和闰年相关的 Bug
- Oracle sqlserver mysql的自增变量设置
- vue 指令 v-once
- android 拉伸view,安卓ImageView拉伸展示
- PIP scrapydo时报错ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log
- python求均值函数_python求列表平均值函数
- 什么快捷键切换仅计算机,什么是电脑屏幕切换快捷键
- 卡特兰数(Catalan UVa 991 10303 10007 1478)[11]
- 优化模型验证关键代码06:多行程旅行商问题(mTSP)
- cpu超线程优缺点_CPU有无超线程重要吗?i7 10700K与9700K对比测试
- 一名小程序员的2021年度 个人总结
- 视频播放插件AVPro1-插件介绍
- 怎样给电脑文件夹批量快速重新命名?
- linux中如何看文件换行符,linux下的换行符
- 下划线的作用(python)
- (附源码)springboot 房产中介系统 毕业设计 312341
热门文章
- HTTP 2.0与HTTP 1.0的区别 ?
- C语言 其他标准函数
- 下pg负载均衡_SAE 场景下,应用流量的负载均衡及路由策略配置实践
- java数据结构有哪些_java有哪些数据结构?
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
- 16、分布式文档系统--document的_source元数据以及定制返回结果解析(来自学习资料+自己整理)
- 自定义Flume拦截器,并将收集的日志存储到Kafka中(案例)
- sqlplus连接远程数据库
- JavaGUI中的JComboBox的处理
- 窗口分析函数_2_生成同值重复排名序号