技术库 > Java

Spring bean scopes 使用

技术库:tec.5lulu.com

from:tec.5lulu.com

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 :

  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. *

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.

  1. package com.mkyong.customer.services;  
  2.    
  3. public class CustomerService   
  4. {  
  5.     String message;  
  6.    
  7.     public String getMessage() {  
  8.         return message;  
  9.     }  
  10.    
  11.     public void setMessage(String message) {  
  12.         this.message = message;  
  13.     }  
  14. }  

1. Singleton example

If no bean scope is specified in bean configuration file, default to singleton.

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.    
  6.        <bean id="customerService"   
  7.             class="com.mkyong.customer.services.CustomerService" />  
  8.    
  9. </beans>  

Run it

  1. package com.mkyong.common;  
  2.    
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.    
  6. import com.mkyong.customer.services.CustomerService;  
  7.    
  8. public class App   
  9. {  
  10.     public static void main( String[] args )  
  11.     {  
  12.         ApplicationContext context =   
  13.          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});  
  14.    
  15.         CustomerService custA = (CustomerService)context.getBean("customerService");  
  16.         custA.setMessage("Message by custA");  
  17.         System.out.println("Message : " + custA.getMessage());  
  18.    
  19.         //retrieve it again  
  20.         CustomerService custB = (CustomerService)context.getBean("customerService");  
  21.         System.out.println("Message : " + custB.getMessage());  
  22.     }  
  23. }  

Output

  1. Message : Message by custA  
  2. Message : 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.

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.    
  6.    <bean id="customerService" class="com.mkyong.customer.services.CustomerService"   
  7.          scope="prototype"/>  
  8.    
  9. </beans>  

Run it again

  1. Message : Message by custA  
  2. Message : 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.

  1. package com.mkyong.customer.services;  
  2.    
  3. import org.springframework.context.annotation.Scope;  
  4. import org.springframework.stereotype.Service;  
  5.    
  6. @Service  
  7. @Scope("prototype")  
  8. public class CustomerService   
  9. {  
  10.     String message;  
  11.    
  12.     public String getMessage() {  
  13.         return message;  
  14.     }  
  15.    
  16.     public void setMessage(String message) {  
  17.         this.message = message;  
  18.     }  
  19. }  

Enable auto component scanning

  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.     http://www.springframework.org/schema/context  
  7.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  8.    
  9.        <context:component-scan base-package="com.mkyong.customer" />  
  10.    
  11. </beans>   

Reference: http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes

Spring bean scopes 使用


本文链接 http://tec.5lulu.com/detail/110djn2ehbgy9852f.html

我来评分 :6.1
0

转载注明:转自5lulu技术库

本站遵循:署名-非商业性使用-禁止演绎 3.0 共享协议

www.5lulu.com