PropertyEditor(Spring1.2.1)
java.beans.PropertyEditorは,String型の値を,実際の何らかの型に変換する際に使用できる.Springでは,これを容易に使用するための仕組みがあるようなので,試してみた.
以下は,本頁のサンプルのbeanクラスである.
プロパティvalueの型が,サンプルのクラスCustomTypeとなっている.
valueをBeanFactoryなどで設定使用とした場合,今までの方法だと,CustomTypeをbeanとして設定し,SampleBeanのbean設定ではそれを要素refなどで参照する.
<bean id="customType" class="org.fireproject.springsample.CustomType">
<constructor-arg><value>100</value></constructor-arg>
</bean>
<bean id="sampleBean" class="org.fireproject.springsample.SampleBean">
<property name="value"><ref bean="customType"/></property>
</bean>
これには以下のような問題がある.
- CustomTypeをあくまで型として扱いたい場合に,bean定義するのは違和感がある.
- 上とも絡むが,CustomType型のプロパティを多く設定する場合,bean定義が多くなり,設定が繁雑になる.
<bean id="sampleBean" class="org.fireproject.springsample.SampleBean">
<property name="value"><value type="org.fireproject.springsample.CustomType">100</value></property>
</bean>
などとしても,結局文字列である"100"をCustomType型に変換できなくてエラーとなる.
要は文字列を独自の型(CustomType)に変換するためのクラス(PropertyEditor)を作成し,組み込む必要がある.
組み込みに関しては,Springはサポートをしているので,あとはそれに乗っかりつつPropertyEditorを作成するだけである.
まず,PropertyEditorを作成する(※).
java.beans.PropertyEditorは,Java標準クラスライブラリのインタフェースで,実装のためのヘルパークラスとして,java.beans.PropertyEditorSupportがある.
今回はこれを拡張して実装した.
CustomTypeのPropertyEditorということで,クラス名はCustomTypeEditorとした.
上の実装では,変則的に,文字列値は":"文字混じりのHEX値であると過程してそれをLongオブジェクトに変換して,CustomTypeオブジェクトを生成している.
この命名規則
対象の型 + Editorに従うと,対象の型のプロパティを生成,設定する際に,SpringはProeprtyEditorを自動検出して使用する. したがって,以下のbeans.xmlにて実行可能となる.
<bean id="sampleBean" class="org.fireproject.springsample.SampleBean">
<property name="value"><value>AB:CD:EF</value></property>
</bean>
SpringはSampleBeanのプロパティvalueがCustomType型であるので,CustomTypeEditorを探して,それを使用して文字列"AB:CD:EF"からCustomTypeオブジェクトを生成してSampleBeanにsetValueする.
この段階でのサンプルの実行結果は以下である.
...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean' springsample.CustomTypeEditor:21 - setAsText( AB:CD:EF ) springsample.CustomTypeEditor:23 - trimmedText = ABCDEF springsample.HelloApplicationContext:36 - org.fireproject.springsample.SampleBean[value = org.fireproject.springsample.CustomType[value = 11259375]]最終的なサンプルとその実行結果は次節を参照されたい.
※ PropertyEditorはSpringとは直接関係ないようなきもするが,サンプルでは重要なポイントとなるし,私自身がよく知らないので書いておく.
せっかくのSpringのDIContainerである.
当然のように,PropertyEditorへのプロパティの設定が可能となっている.
以下に,本頁の最終的なサンプルのbeans.xmlを示す.
前節までのbeans.xmlの記述でいらないものは,コメントアウトしている.
id値customEditorConfigurerのbeanのプロパティcustomEditorsに,独自に作成したcustomEditorを設定していく.
最後に,サンプルのドライバクラスを以下に示す.
id値sampleBeanのbeanを取り出してログ出力するだけである.
実行結果を以下に示す.
<bean id="customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="org.fireproject.springsample.CustomType">
<bean class="org.fireproject.springsample.CustomTypeEditor">
<property name="prefix" value="<<"/>
<property name="suffix" value=">>"/>
</bean>
</entry>
</map>
</property>
</bean>
customEditorConfigurerのプロパティcustomEditorsはmap型で,複数のPropertyEditorを設定できるようになっている.
キーと値の設定は以下の要にする.
- キー
- 要素entryの属性keyに,登録するPropertyEditorが編集対象とする型を記述する.
- 値
- 要素entryの子要素beanの属性classにPropertyEditorクラスを記述する. この要素beanにて,propertyを設定できる.
$> java -jar springsample.jar ...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'customEditorConfigurer' ...省略... support.AbstractBeanFactory:219 - Creating shared instance of singleton bean 'sampleBean' springsample.CustomTypeEditor:21 - setAsText( AB:CD:EF ) springsample.CustomTypeEditor:23 - trimmedText = ABCDEF springsample.HelloApplicationContext:36 - org.fireproject.springsample.SampleBean [value = org.fireproject.springsample.CustomType[value = <<11259375>>]]CustomTypeEditorにprefix,suffixが設定されていることが分かる.

