MethodInvokingFactoryBeanによるbeanの生成やその前処理(Spring1.2.1)
MethodInvokingFactoryBeanを使用すると,beanの生成やその前処理の設定が,容易に行えるらしい.
MethodInvokingFactoryBeanはFactoryBeanである.
すなわちbeanを生成するメソッドがあり,前に見てきたFieldRetrievingFactoryBeanの様な形式で,そのメソッドを指定することができる.
指定できるメソッドは,staticなもの,非staticなものの両方である.
staticなメソッド呼び出しの設定は以下である.
- targetClass
- staticフィールドを持つクラス
- targetMethod
- staticフィールド名
- staticMethod
- targetClassとtargetMethodによる指定を一度に指定する.
<bean id="initBeanStatic1"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod">
<value>org.fireproject.springsample.SampleInitializer.staticInit</value>
</property>
</bean>
<bean id="sampleBean1"
class="org.fireproject.springsample.SampleBean"
depends-on="initBeanStatic1">
</bean>
そしてtargetClassとtargetMethodによる指定.
<bean id="initBeanStatic2"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass">
<value>org.fireproject.springsample.SampleInitializer</value>
</property>
<property name="targetMethod">
<value>staticInit</value>
</property>
</bean>
<bean id="sampleBean2"
class="org.fireproject.springsample.SampleBean"
depends-on="initBeanStatic2">
</bean>
上のどちらの例でも,FactoryBeanとしてではなく,別beanからdepends-onするbeanとして指定している.
こうすることによって,あるbeanを作成する前に,何らかの初期化処理をするような設定が実現できる.
実はSpringの本家のドキュメントによると,Factoryパターンの適用には,MethodInfokingFactoryBeanよりもfactory-methodsを使用する方法を推奨している.
そうするとMethodInvokingFactoryBeanの用途は,このような初期化のみとなる.
非staticメソッドを指定する場合は,以下の二つを設定する.
- targetObject
- 呼び出すメソッドを持つオブジェクト
- targetMethod
- 指定するメソッドの名前
<bean id="initBeanNonStatic"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean class="org.fireproject.springsample.SampleInitializer"/>
</property>
<property name="targetMethod">
<value>nonStaticInit</value>
</property>
</bean>
<bean id="sampleBean3"
class="org.fireproject.springsample.SampleBean"
depends-on="initBeanNonStatic">
</bean>
targetObjectの子要素では,直接bean設定することにより,そのbeanを指定している.
ここは要素refなどを使用することもできる.
以下に,本頁のサンプルのbeans.xmlを示す.
前節までの例が記述されている.
さらにFactoryBeanとしても使用できることを確認するための例も追加した.
メソッドが呼ばれたらログ出力するだけである.
サンプルドライバは以下.
実行してみた.
<bean id="sampleBean4"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass">
<value>org.fireproject.springsample.SampleInitializer</value>
</property>
<property name="targetMethod">
<value>createSampleBean</value>
</property>
</bean>
SampleInitializerは以下.
$> java -jar springsample.jar ...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'initBeanStatic1' springsample.SampleInitializer:10 - org.fireproject.springsample.SampleInitializer staticInit support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean1' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'initBeanStatic2' springsample.SampleInitializer:10 - org.fireproject.springsample.SampleInitializer staticInit support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean2' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'initBeanNonStatic' springsample.SampleInitializer:14 - org.fireproject.springsample.SampleInitializer nonStaticInit support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean3' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean4' springsample.HelloApplicationContext:38 - getBean(sampleBean1) springsample.HelloApplicationContext:40 - org.fireproject.springsample.SampleBean@2a4983 springsample.HelloApplicationContext:38 - getBean(sampleBean2) springsample.HelloApplicationContext:40 - org.fireproject.springsample.SampleBean@406199 springsample.HelloApplicationContext:38 - getBean(sampleBean3) springsample.HelloApplicationContext:40 - org.fireproject.springsample.SampleBean@c7b00c springsample.HelloApplicationContext:38 - getBean(sampleBean4) springsample.HelloApplicationContext:40 - org.fireproject.springsample.SampleBean@1f6f296

