[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][JSP] EclipseでのJSP作成
今回は、Eclipseを用いてJSPを作成してみます。 まず、Eclipseを起動します。
-
-
[Java] Javaのインストール(プログラム開発・入門用)
Javaのコードを書きたい!! という目的のためのJavaのインストール方法と、コマンドラインでの
-
-
[Java][Servlet] EclipseでのJavaServlet作成
こんにちは、今回はEclipseを使用してJavaServletを作成します。 Eclipse
-
-
[java] 文字列の一部を切り取る方法(substring)
こんにちは。 今回は文字列の一部を切り取る方法について解説します。 具体的には、String
-
-
[Java][MySQL] JavaからMySQLを使う方法
MySQLは、フリーでありながら商用としても使用できるパワーを持つデータベース(RDB)です。
-
-
[Java] StringBuilderの使い方(Stringを連結するクラス)
今回は、可変長文字列クラスのStringBuilderを紹介します。 固定長文字列クラスのSt
-
-
[Java][Spring] Spring3でDIしてみる!(アノテーション使用)
Spring3を使って、一番基本的な形のプログラムを作成してみます。 今回は、アノテーションを
-
-
[Java] Apache POIでExcel操作!
Apache POIをまずは使ってみようという方(自分も含め)のメモ 導入 Maven htt
-
-
[Java] 日付の計算方法(DateFormat、Date、Calendar)
こんにちは、kei0310です。 Javaで日付を計算する方法について解説します。 まず
-
-
[Java][Jersey] JerseyでRESTfulなJAX-RS Webサービス作成
こんにちは、keiです。 今回はRESTfulなWeb Serviceの作成を行っていきます。