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/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()方法获取它,它总是返回同一个实例。
- <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
- 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>
Spring Bean作用域实例的更多相关文章
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )
前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...
- Spring bean作用域
全当知识要点记录了,大家随意踩踩. spring的作用域有以下几种singleton作用域prototype作用域request作用域session作用域global-session作用域 1. si ...
- [spring] -- bean作用域跟生命周期篇
作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...
- java Spring bean作用域
1. Singleton作用域 当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...
- Spring Bean 作用域
Bean 的作用域 当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项.例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean ...
- 第31章 Spring bean 作用域
每日一句 I must say a word about fear. It is life's only true opponent. Only fear can defeat life. 这里必须说 ...
- Spring学习手札(四)谈谈Spring Bean的生命周期及作用域
在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...
- Spring bean相关
Spring中指定Bean的作用于的方式 以下四种为例: 单例(默认,可以不用特殊表明) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) ...
随机推荐
- Java Eclipse 配置
1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...
- [转载]Windows服务编写原理及探讨(1)
有那么一类应用程序,是能够为各种用户(包括本地用户和远程用户)所用的,拥有用户授权级进行管理的能力,并且不论用户是否物理的与正在运行该应用程序的计算机相连都能正常执行,这就是所谓的服务了. (一)服务 ...
- 在Ubuntu上使用pip安装错误 read timed out 处理方法
在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.
- Tutorial 1: Serialization
转载自:http://www.django-rest-framework.org/tutorial/1-serialization/#tutorial-1-serialization Tutorial ...
- Debian系网络配置 /etc/network/interfaces
说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面,要修改?你一个一个文件来过吧.Debian系 ...
- IDEA添加其他项目为库文件的方法
IDEA添加其他项目为库文件的方法 2014年1月23日星期四 20:46 问题:如果我通过引用项目修改了被引用库项目的源代码,他没法自动编译,所以不会立即反应到引用项目. 在Eclipse中,很简单 ...
- log优化
isLoggable(Level level) 包含计算的日志记录用isLoggable判断下. debug info warn error ,一般记录error, 但是其他里面的计算还是 ...
- 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)
问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器
- 字节对齐&&sizeof
转:http://blog.chinaunix.net/uid-722885-id-124878.html 1. sizeof应用在结构上的情况 请看下面的结构: struct MyStruct { ...
- SGU 217. Two Cylinders
题意:给空间内两根圆柱,求轴线垂直相交时公共部分的体积. 暴力积分即可. ID: Date'n'Time: Name: Task: .Ext: Status: Time: Memory: 158937 ...