Spring beanスコープの例
Springでは、Beanスコープを使用して、Springコンテナから呼び出し元に返すBeanインスタンスのタイプを決定します。
サポートされている5種類のBeanスコープ:
-
singleton – Spring IoCコンテナごとに単一のBeanインスタンスを返す
-
prototype – リクエストされるたびに新しいBeanインスタンスを返す
-
request – HTTPリクエストごとに単一のBeanインスタンスを返します. **
-
session – HTTPセッションごとに1つのBeanインスタンスを返します. **
-
globalSession – グローバルHTTPセッションごとに単一のBeanインスタンスを返します.
**
ほとんどの場合、Springのコアスコープ(シングルトンとプロトタイプ)のみを扱うことができ、デフォルトスコープはシングルトンです。
P.S ** は、Web対応のSpring ApplicationContext
のコンテキストでのみ有効であることを意味します
シングルトン対プロトタイプ
ここでは、Beanスコープの違いを示す例を示します。
package com.mkyong.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.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’はシングルトンスコープにあるため、 ‘custB’による2回目の検索では、新しいgetBean()メソッドで取得した場合でも ‘custA’によって設定されたメッセージが表示されます。 Singletonでは、Spring IoCコンテナごとに1つのインスタンスしかなく、getBean()で何回取得しても、常に同じインスタンスが返されます。
2.プロトタイプの例
新しい ‘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()`メソッドの新しいインスタンスが作成されます。
3.ビーンスコープの注釈
注釈を使用してBeanのスコープを定義することもできます。
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>
ソースコードをダウンロードする
ダウンロードする – リンク://wp-content/uploads/2010/03/Spring-Bean-Scopes-Example.zip[Spring-Bean-Scopes-Example.zip](7 KB)