SpringAnnotation 中的Scope参数
来源:互联网 发布:数据库主键和外键创建 编辑:程序博客网 时间:2024/05/24 06:29
在Spring中,Bean的Scope参数值用于决定访问者的Bean示例应该以哪种方式返回Spring容器调用方法
Bean的Scope参数支持五种类型
- singleton--按照Spring的IOC容器返回一个单Bean实例
- prototype--每当请求的时候返回一个新的Bean实例
- request--按照HTTP Request返回一个单一的Bean实例
- session--按照HTTP Session返回一个单一的Bean实例
- globalSession--按照Global HTTP Session 返回一个单一的Bean实例
在大多数情况下 ,只需要处理Spring的核心的Scpoe singleton(单例模式)和prototype(原型模式)
Singleton和prototype的区别
package com.mkyong.customer.services; public class CustomerService {String message; public String getMessage() {return message;} public void setMessage(String message) {this.message = message;}}
如果不在Scop中指定范围默认的是singleton
<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.mkyong.customer.services.CustomerService" /> </beans>
测试
package com.mkyong.common; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mkyong.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 custAMessage : Message by custA
从bean后'customerService'是Singleton的范围,第二检索通过'custB'将显示所设置的消息'custA'还有,甚至getBean新方法检索。 lleton,仅为单个实例SpringIoC容器,无论多少时间你检索getBean,它将始终返回相同的实例。
Prototype
如果你想要一个新的'customerService'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/beanshttp://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 custAMessage : null
在原型范围,您必须为每个getBean一个新实例调用的方法。
Annotation定义scop参数
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/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.mkyong.customer" /> </beans
- SpringAnnotation 中的Scope参数
- SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope
- angularjs中的$scope参数
- oracle设置初始化参数中的scope子句
- 有关oracle数据库中的scope参数
- AngularJS 中的scope($scope)
- SPFILE 指定SCOPE参数
- Maven scope 参数含义
- scope参数值
- scope参数错误或没有scope权限
- Scope参数错误或没有Scope权限
- JSTL中的scope属性
- Spring 中的 Bean Scope
- spring中的scope
- maven中的<scope>
- Spring中的Scope详解
- spring中的scope
- spring中的scope
- 10大iOS开发者最喜爱的类库
- android webview的使用
- C#路径的八种相关操作
- C++ stl编程之Vector删除后iterator可能失效
- Android设备连接豌豆荚等显示的设备名
- SpringAnnotation 中的Scope参数
- asp.net清除页面缓存,防止页面回退重复提交数据及防止同一用户同时登录
- 有关usb的一些资料
- PHP中获取文件扩展名的N种方法
- TabActivity可以显示实现多页显示效果,在xml布局文件中需要有一些特殊的配置属性
- 泛型lua的for循环以及lua的特殊的dowhile循环
- Android底部菜单栏 仿微博效果
- PM俱乐部建设之旅5-第4~6次会议
- Linux 判断是否是一个目录还是一个文件的方法