Spring(5)-Spring bean scopes example
来源:互联网 发布:js记录页面刷新次数 编辑:程序博客网 时间:2024/06/07 20:16
In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller.
5 types of bean scopes supported :
- singleton – Return a single bean instance per Spring IoC container
- prototype – Return a new bean instance each time when requested
- request – Return a single bean instance per HTTP request. *
- session – Return a single bean instance per HTTP session. *
- globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
P.S * means only valid in the context of a web-aware Spring ApplicationContext
Singleton vs Prototype
Here’s an example to show you what’s the different between bean scope : singleton and prototype.
package com.mkyong.customer.services; public class CustomerService {String message; public String getMessage() {return message;} public void setMessage(String message) {this.message = message;}}
1. Singleton example
If no bean scope is specified in bean configuration file, default to 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>
Run it
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()); }}
Output
Message : Message by custAMessage : Message by custA
Since the bean ‘customerService’ is in singleton scope, the second retrieval by ‘custB’ will display the message set by ‘custA’ also, even it’s retrieve by a new getBean() method. In singleton, only a single instance per Spring IoC container, no matter how many time you retrieve it with getBean(), it will always return the same instance.
2. Prototype example
If you want a new ‘customerService’ bean instance, every time you call it, use prototype instead.
<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>
Run it again
Message : Message by custAMessage : null
In prototype scope, you will have a new instance for eachgetBean()
method called.
3. Bean scopes annotation
You can also use annotation to define your bean scope.
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;}}
Enable auto component scanning
<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>
Reference: http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes
- Spring Bean Scopes Example
- Spring bean scopes example
- Spring(5)-Spring bean scopes example
- Spring Bean scopes
- Spring bean scopes
- spring bean scopes
- Spring Bean Scopes作用域
- Spring bean reference example
- Spring in Action 3 - Spring’s bean scopes
- Java Spring Tutorial -- Singleton and Prototype Bean Scopes
- 【Java.Spring.Core】【IoC】Bean 作用域 - Scopes
- Spring中scopes属性。
- Spring中的Scopes
- Spring Bean Scope Example using @Scope Annotation
- Bean scopes
- Bean scopes
- spring example
- Spring Bean Validation Example with JSR-303 Annotations
- win7&ubuntu全攻略(仅适用于正版win7系统)
- 堆排序(Heap Sort)算法的实现
- 如何在xp win7中設置vpn(美國VPN)
- 图邻接表类(图的遍历方法,最短距离及路径)
- Ajax请求方式区别
- Spring(5)-Spring bean scopes example
- Quick Sort (快速排序)
- Ubuntu系统下查看版本信息
- 刚刚做好LFS(6.2),做个总结
- 数据结构专题——线段树
- win32sdk学习-创建一个对话框窗口
- KDE vs. GNOME:一位用户的经验之谈
- 浅尝Eclipse Memory Analyzer(MAT)
- 算法图解