SpringAnnotation 中的Scope參數

在Spring中,Bean的Scope參數值用於決定訪問者的Bean示例應該以哪種方式返回Spring容器調用方法

Bean的Scope參數支持五種類型

  1. singleton--按照Spring的IOC容器返回一個單Bean實例
  2. prototype--每當請求的時候返回一個新的Bean實例
  3. request--按照HTTP Request返回一個單一的Bean實例
  4. session--按照HTTP Session返回一個單一的Bean實例
  5. 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/beans
	http://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 custA
Message : 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/beans
	http://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 custA
Message : 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/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.mkyong.customer" />
 
</beans



 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章