Spring Bean作用域範例


在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.tw511.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://pan.baidu.com/s/1o7jxMrg