[Java][Spring] DIする方式を細かく見てみる① @ScopeでのSingletonとPrototypeの指定
今回は@Scopeアノテーションを用いて、オブジェクトをDIする方式を選択してみます。
●解説
@Scopeで指定可能な属性は以下となります。
@Scope("singleton") :
省略可能。Springコンテナ内に単一オブジェクトを生成し、常にそのオブジェクトがDIされる。
@Scope("prototype") :
指定必須。DIされる度にオブジェクトをnewする。
●サンプルプログラム
こちらの記事のサンプルプログラムを拡張しています。
・TestService.java
@scopeを省略しているので、singletonでDIされるオブジェクトとなります。
package test.spring3;
import org.springframework.stereotype.Component;
@Component
public class TestService {
private int value = 0;
public void exec(){
System.out.println("exec()が実行されました。値は[" + this.value + "]です。");
}
public void setValue(int value){
this.value = value;
}
}
・TestServicePrototype.java
@scopeでprototypeの指定をしているので、毎回newでDIされるオブジェクトとなります。
package test.spring3;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class TestServicePrototype {
private int value = 0;
public void exec(){
System.out.println("exec()が実行されました。値は[" + this.value + "]です。");
}
public void setValue(int value){
this.value = value;
}
}
・App.java
TestServiceとTestServicePrototypeを2個ずつDIして動きの違いを確認しています。
package test.spring3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class App {
@Autowired
private TestService service1 = null;
@Autowired
private TestService service2 = null;
@Autowired
private TestServicePrototype servicePrototype1 = null;
@Autowired
private TestServicePrototype servicePrototype2 = null;
public void execService(){
service1.setValue(1);
service2.setValue(2);
service1.exec();
service2.exec();
System.out.println("service1とservice2は同じオブジェクト(Singleton)なので同一の値が出力される。");
servicePrototype1.setValue(1);
servicePrototype2.setValue(2);
servicePrototype1.exec();
servicePrototype2.exec();
System.out.println("servicePrototype1とservicePrototype2は別のオブジェクト(Prototype)なので別の値が出力される。");
}
public static void main( String[] args ){
// Springコンテナ取得
ApplicationContext appContext =
new ClassPathXmlApplicationContext("classpath:./META-INF/spring/app-context.xml");
// SpringコンテナからAppクラスのオブジェクトを取得
App app = appContext.getBean(App.class);
// appを実行
app.execService();
}
}
・実効結果
実効結果は以下のとおりです。
singletonとprototypeの動きの違いが確認できます。
2013/12/02 13:20:56 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@38503429: startup date [Mon Dec 02 13:20:56 JST 2013]; root of context hierarchy
2013/12/02 13:20:56 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [META-INF/spring/app-context.xml]
2013/12/02 13:20:56 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
情報: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@217f242c: defining beans [app,testService,testServicePrototype,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
exec()が実行されました。値は[2]です。
exec()が実行されました。値は[2]です。
service1とservice2は同じオブジェクト(Singleton)なので同一の値が出力される。
exec()が実行されました。値は[1]です。
exec()が実行されました。値は[2]です。
servicePrototype1とservicePrototype2は別のオブジェクト(Prototype)なので別の値が出力される。
スポンサードリンク
関連記事
-
[Java][Framework] Spring BootのJPAでDB接続した際のEntityキャッシュのクリア方法
Spring BootのJPAでDBアクセスした際に、処理を一定量こなすと、Heapが足りなくな
-
[Java] 日付の計算方法(DateFormat、Date、Calendar)
こんにちは、kei0310です。 Javaで日付を計算する方法について解説します。 まず
-
[Java][基本] mainメソッドの書き方
こんにちはkeiです。 Javaの基本として、意外と知らないmain関数について解説します。
-
[Java] テキストファイルの読み込み方法
こんにちは、今回はテキストファイルをJavaで読む方法を紹介します。 標準で提供されているクラ
-
[Java][Servlet] EclipseでのJavaServlet作成
こんにちは、今回はEclipseを使用してJavaServletを作成します。 Eclipse
-
[Java][Spring] Spring3でDIしてみる!(アノテーション使用)
Spring3を使って、一番基本的な形のプログラムを作成してみます。 今回は、アノテーションを
-
[Java][Servlet][JSP] ServletからJSPへの転送
こんにちは。 今回は、「Servletでリクエストを受けて、JSPで表示する。」方法を解説しま
-
[Java][基本] ループ処理(for文)
Javaのループ処理の基本である、for文を解説します。 ●文法 for( [①初期化];
-
[Java] CSVファイルの読み込み方法
こんにちは、kei0310です。 CSVファイルをJavaに取り込む方法を解説します。
-
[Java] クラスとインスタンス
こんにちは。 クラスとインスタンスについて解説します。 インスタンスは「オブジェクト」とも言