当前位置: 首页 > news >正文

Spring中bean的范围

Spring中有5种bean的范围:

5 types of bean scopes supported :

  1. singleton – Return a single bean instance per Spring IoC container 这个范围也是默认的
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *

P.S * means only valid in the context of a web-aware Spring ApplicationContext

我们看个关于singleton and prototype.的例子:

public class CustomerService 
{
	String message;
 
	public String getMessage() {
		return message;
	}
 
	public void setMessage(String message) {
		this.message = message;
	}
}

1. Singleton example

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
       <bean id="customerService" 
            class="com.mkyong.customer.services.CustomerService" />
 
</beans>

  运行:

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 again
    	CustomerService custB = (CustomerService)context.getBean("customerService");
    	System.out.println("Message : " + custB.getMessage());
    }
}

  输出为:

Message : Message by custA
Message : Message by custA

2. Prototype example

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
   <bean id="customerService" class="com.mkyong.customer.services.CustomerService" 
         scope="prototype"/>
 
</beans>

  运行输出为:

Message : Message by custA
Message : null

也可以使用注解来做这些事情:
package com.mkyong.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/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
       <context:component-scan base-package="com.mkyong.customer" />
 
</beans>

  

相关文章:

  • zsh 配置文件
  • linux监控cacti配置教程(6)
  • 【APACHE】如何重启Apache?
  • vim 命令(全) 03
  • 信号和槽机制
  • SQL Server 2008 R2 安装出错:Could not open key
  • PHP最佳实践【转载】
  • 遍历查询ldap服务器用户
  • ARM推出全球功耗效率最高的64位处理器Cortex-A50系列
  • Android查看stdout 和stderr
  • png图片格式 (share)
  • uva 106 Fermat vs. Pythagoras
  • [C++] cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
  • 老说技术更迭快,可十年到底可以淘汰多少知识?
  • 统计登录人数
  • 分享一款快速APP功能测试工具
  • E-HPC支持多队列管理和自动伸缩
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Python十分钟制作属于你自己的个性logo
  • React16时代,该用什么姿势写 React ?
  • 对象管理器(defineProperty)学习笔记
  • 番外篇1:在Windows环境下安装JDK
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 深度学习中的信息论知识详解
  • 移动端唤起键盘时取消position:fixed定位
  • - 转 Ext2.0 form使用实例
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #Linux(Source Insight安装及工程建立)
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (2)STM32单片机上位机
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (一)UDP基本编程步骤
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .cfg\.dat\.mak(持续补充)
  • .FileZilla的使用和主动模式被动模式介绍
  • .htaccess 强制https 单独排除某个目录
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET Core引入性能分析引导优化
  • .NET Micro Framework 4.2 beta 源码探析
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .NET中GET与SET的用法
  • @在php中起什么作用?
  • [2010-8-30]
  • [Angular 基础] - 指令(directives)
  • [BeginCTF]真龙之力
  • [C++随笔录] 红黑树
  • [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算
  • [ExtJS5学习笔记]第三十节 sencha extjs 5表格gridpanel分组汇总