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.zaixian.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.zaixian.customer.services.CustomerService" />

</beans>

執行結果:

package com.xuhuhu.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zaixian.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.zaixian.customer.services.CustomerService"
         scope="prototype"/>

</beans>

運行-執行

Message : Message by custA
Message : null
在原型作用域,必須為每個 getBean()方法中調用返回一個新的實例。

3. Bean作用域注釋

還可以使用注釋來定義 bean 的作用域。
package com.zaixian.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.zaixian.customer" />

</beans>

上一篇: Spring內部bean實例 下一篇: Spring集合 (List,Set,Map,Properties) 實例