容器中Bean的作用域
来源:互联网 发布:int对应的范围c语言 编辑:程序博客网 时间:2024/05/21 09:17
通过Spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域。
Spring支持如下5种作用域:
> singleton:单例模式,在整个Spring IoC 容器中,使用singleton定义的Bean将只有一个实例。不指定Bean的作用域时,Spring默认使用singleton作用域。
> prototype:原型模式,每次通过容器的getBean方法获取prototype 定义的Bean时,都将产生一个新的Bean实例。
> request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。只有在Web应用中使用Spring时,该作用域才真正有效。
> session:对于每次HTTP Session,使用session定义的Bean都将产生一个新实例,即每次HTTP Session都将会产生不同的Bean实例。只有在Web应用中使用Spring时,该作用域才真正有效。
> global session:每个全局的HTTP Session对应一个Bean实例。典型情况下,仅在使用portlet context的时候有效。只有在Web应用中使用Spring时,该作用域才真正有效。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><!-- 配置一个singleton Bean实例 --><bean id="p1" class="demo.PersonDemo"/><!-- 配置一个prototype Bean实例 --><bean id="p2" class="demo.PersonDemo" scope="prototype"/></beans>
package demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeTest {public static void main(String[] args) throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");System.out.println(ctx.getBean("p1")==ctx.getBean("p1"));System.out.println(ctx.getBean("p2")==ctx.getBean("p2"));}}
阅读全文
0 0
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- 容器中 Bean 的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- 容器中Bean的作用域
- Spring容器中Bean的作用域
- 7.5.2:容器中Bean的作用域
- 四、Spring容器中bean的作用域
- spring容器中bean的作用域bean的后处理器
- Spring容器Bean的作用域
- Spring中bean的作用域及Spring容器初始化的相关问题
- ElasticSearch维护-备份和恢复
- 使用 takephoto_library 踩得坑
- 蓝桥杯 算法提高 题目1 最大最小值
- 消息队列-ActiveMQ学习笔记(三)-发布-订阅消息模式实现
- 2017第十四届中国(上海)国际工业自动化及工业机器人展览会会刊(参展商名录)
- 容器中Bean的作用域
- java获取webp格式图片宽和高
- js-图片切换实例
- Linux 常用命令记录
- H5页面
- 解决安装MySQL进行写入配置文件时卡住或报错问题
- 我是如何入门Git?
- Python 初体验 基本语法
- Mvp实战心得(二)---Base基类的封装