PropertyPathFactoryBeanによるプロパティの簡単指定(Spring1.2.1)
PropertyPathFactoryBeanというFactoryBeanを使用すると,プロパティをより簡単に指定,取得できるらしいので,試してみた.
PropertyPathFactoryBeanはFactoryBeanである.
PropertyPathFactoryBeanには,以下の三つのプロパティを設定して使用する.
- targetBeanName
- propertyPathの起点となるbeanのid.
- targetObject
- propertyPathの起点となるbeanのオブジェクト.
- propertyPath
- targetBeanNameで指定したbeanのプロパティをパスのような形式で指定する.
hoge.fuga.foo.varといった指定をすると,targetBeanNameで示されるbeanかtargetObjectからgetHogeして取得したオブジェクトからgetFugaして取得したオブジェクトからgetFooしたオブジェクトからgetVarしたオブジェクトが,このbean指定(FactoryBeanであるPropertyPathFactoryBeanから)で取得できるbeanである.
以下に,本頁のサンプルのbeans.xmlを示す.
まず,java.io.Fileを使用してbeanを指定している.
<bean id="targetDir" class="java.io.File">
<constructor-arg><value>/etc/init.d/networking</value></constructor-arg>
</bean>
Fileには,getParentFileメソッドと,getPathメソッドがある.
したがって,FileにはparentFileとpathというプロパティがあるものとして,PropertyPath指定ができる.
<bean id="parent1"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>targetDir</value>
</property>
<property name="propertyPath">
<value>parentFile.path</value>
</property>
</bean>
id値parent1のbeanでは,targetBeanNameでtargetDirを指定している.
したがって,id値targetDirのbeanを起点にPropertyPathがparentFile.pathというプロパティが,id値parent1のbeanとなる.
具体的には,
(targetDirのbean).getParentFile().getPath()で取得できる値である.
PropertyPath指定による設定の応用例として,まずPropertyPathFactoryBeanによるbean自体で内包するbeanを起点にPropertyPath指定する方法がある.
最後にサンプルの実行結果を示す.
<bean id="level"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject">
<bean class="org.fireproject.springsample.SampleBean">
...省略...
</bean>
</property>
<property name="propertyPath">
...省略...
</property>
</bean>
上の例では,プロパティ値targetObject内で直接別beanを設定し,それに対してpropertyPath指定している.
もちろんtargetObjectで要素refを使用することもできる.
次に,beanのid値にていきなり起点のbeanを含めたPropertyPathを指定する省略記法である.
<bean id="targetDir.parentFile.parentFile.path"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
この記法は,
<bean id="..."
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>targetDir</value>
</property>
<property name="propertyPath">
<value>parentFile.parentFile.path</value>
</property>
</bean>
という記法に一致する.
最後に,当然ながら,あるbeanのコンストラクタ引数に,PropertyPathFactoryBeanを使用したbean指定を使用する方法がある.
<bean id="rootDir" class="java.io.File">
<constructor-arg>
<bean id="targetDir.parentFile.parentFile.parentFile.path"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</constructor-arg>
</bean>
id値rootDirで指定されるbeanのコンストラクタには,
targetDir.getParentFile().getParentFile().getParentFile().getPath()の値が渡される. 以下に本頁のサンプルドライバを示す.
$> java -jar springsample.jar ...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'targetDir' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'parent1' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'parent2' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'level' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'targetDir.parentFile.parentFile.path' support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'rootDir' springsample.HelloApplicationContext:41 - /etc/init.d/networking springsample.HelloApplicationContext:41 - /etc/init.d springsample.HelloApplicationContext:41 - /etc springsample.HelloApplicationContext:41 - 5 springsample.HelloApplicationContext:41 - /etc springsample.HelloApplicationContext:41 - /最後の方の"5"という出力は,SampleBeanのgetLevelメソッドによる. SampleBeanは,入れ子構造を取り,getLebelはその何番目の階層かを示している.

