在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者。bean支持的5种范围域:
  1. 单例 - 每个Spring IoC 容器返回一个bean实例
  2. 原型- 当每次请求时返回一个新的bean实例
  3. 请求 - 返回每个HTTP请求的一个Bean实例
  4. 会话 - 返回每个HTTP会话的一个bean实例
  5. 全局会话- 返回全局HTTP会话的一个bean实例
在大多数情况下,可能只处理了 Spring 的核心作用域 - 单例和原型,默认作用域是单例。
注:意味着只有在一个基于web的Spring ApplicationContext情形下有效!
单例VS原型
这里有一个例子来说明,bean的作用域单例和原型之间的不同:
package com.yiibai.customer.services;

public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
1.单例例子
如果 bean 配置文件中没有指定 bean 的范围,默认为单例。
<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.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 again
CustomerService 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()方法获取它,它总是返回同一个实例。

2.原型例子
如果想有一个新的 “CustomerService”bean 实例,每次调用它的时候,需要使用原型(prototype)来代替。
<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.yiibai.customer.services.CustomerService"
scope="prototype"/> </beans>

运行-执行

Message : Message by custA
Message : null
在原型作用域,必须为每个 getBean()方法中调用返回一个新的实例。
3. Bean作用域注释
还可以使用注释来定义 bean 的作用域。
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/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.yiibai.customer" /> </beans>
 
http://www.yiibai.com/spring/spring-bean-scopes-examples.html

最新文章

  1. 屌丝程序员的梦想 (二) 屌丝IT梦开始地方
  2. 使用Fusioncharts实现后台处理进度的前台展示
  3. 如何在Hdevelop加入自己的算子
  4. C#利用iComparable接口实现List排序
  5. Linux环境下发布项目(Tomcat重新启动)
  6. iOS中使用FMDB事务批量更新数据库
  7. linux 下载软件
  8. HDU-4631 Sad Love Story 平面最近点对
  9. 比較具体的handle机制
  10. (转载)PHP 动态生成表格
  11. Mysql 索引的基础(下)
  12. jq入门--选择器
  13. R语言︱list用法、批量读取、写出数据时的用法
  14. Android的图片,字符串,demin,color,以及Array,boolean,Integer资源的使用-android学习之旅(五十四)
  15. HTML5标签选择,图文混排使用dl dt dd
  16. GitHub Desktop 出现“please upgrade your plan to create a new private repository”的解决办法
  17. AD域与外部网站域名相同处理办法
  18. 三篇文章带你极速入门php(一)之语法
  19. Winform开发框架之简易工作流设计(转自 伍华聪博客)
  20. iptables 常用命令

热门文章

  1. 根据名字杀死进程Killall
  2. 使用Guava retryer优雅的实现接口重试机制
  3. 小程序授权怎么写 , 用户点击取消授权 调用 wx.authorize
  4. Centos bash: make: command not found
  5. React.js学习之理解JSX和组件
  6. vim编码相关
  7. XML DOM(Document Object Model)
  8. struts2框架的大致处理流程
  9. 验证Xcode真伪的方法,来自苹果官网
  10. FastReport.Net使用:[34]小册子报表(奇偶页)