FieldRetrievingFactoryBeanによるフィールドの簡単指定(Spring1.2.1)
FieldRetrievingFactoryBeanを使用すると,フィールドをより簡単に指定,取得できるらしいので試してみた.
PropertyPathFactoryBeanと同様,FieldRetrievingFactoryBeanはFactoryBeanである.
FieldRetrievingFactoryBeanは,staticフィールド,非staticフィールドを指定,取得し,beanとして返したり,beanのプロパティとして設定できる.
指定するフィールドがstaticの場合,以下を指定する.
- targetClass
- staticフィールドを持つクラス
- targetField
- staticフィールド名
- staticField
- targetClassとtargetFieldによる指定を一度に指定する.
<bean id="sampleBeanStaticField"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>org.fireproject.springsample.SampleBean.STATIC_INSTANCE</value>
</property>
</bean>
<bean id="sampleBeanStaticField2"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetClass">
<value>org.fireproject.springsample.SampleBean</value>
</property>
<property name="targetField">
<value>STATIC_INSTANCE</value>
</property>
</bean>
さらにstaticFieldによる指定を,属性idのみで指定する省略記法もある.
<bean id="org.fireproject.springsample.SampleBean.STATIC_INSTANCE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
非staticフィールドを指定する場合は,以下の二つを設定する.
- targetObject
- フィールドを持つオブジェクト
- targetField
- 指定するフィールドの名前
<bean id="sampleBean"
class="org.fireproject.springsample.SampleBean">
<property name="innerBean">
<bean id="innerSampleBean"
class="org.fireproject.springsample.SampleBean"/>
</property>
</bean>
<bean id="sampleBeanNonStaticField"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetObject">
<ref bean="sampleBean"/>
</property>
<property name="targetField">
<value>innerBean</value>
</property>
</bean>
以下に,本頁のサンプルのbeans.xmlを示す.
前節までの例が記述されている.
SampleBeanは以下.
サンプルドライバは以下.
実行したみた.
$> java -jar springsample.jar ...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBeanStaticField' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBeanStaticField2' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'org.fireproject.springsample.SampleBean.STATIC_INSTANCE' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBeanNonStaticField' springsample.HelloApplicationContext:38 - getBean(sampleBeanStaticField) springsample.HelloApplicationContext:40 - SampleBean-STATIC_INSTANCE(1)[innerBean = null] springsample.HelloApplicationContext:38 - getBean(sampleBeanStaticField2) springsample.HelloApplicationContext:40 - SampleBean-STATIC_INSTANCE(1)[innerBean = null] springsample.HelloApplicationContext:38 - getBean(org.fireproject.springsample.SampleBean.STATIC_INSTANCE) springsample.HelloApplicationContext:40 - SampleBean-STATIC_INSTANCE(1)[innerBean = null] springsample.HelloApplicationContext:38 - getBean(sampleBeanNonStaticField) springsample.HelloApplicationContext:40 - SampleBean-innerSampleBean(2)[innerBean = null]

