J2SE6と式評価エンジン(その2)
例の善し悪しは別にして、DIの式評価エンジンにMozilla Rhino:Java Script for Javaを使うとこんなことも出来てしまう。(設定ファイルは開発中のものなので適当)
<components> <component name="mainFrame" class="javax.swing.JFrame" instance="singleton" autoBinding="none"> <property name="Visible">true</property> <method name="setDefaultCloseOperation"> <arg><js>JFrame.EXIT_ON_CLOSE</js></arg> </method> <method name="add"> <arg>panel1</arg> </method> <method><js>self.setBounds(10, 10, 400, 100);</js></method> <method><js>self.setTitle("メソッドインジェクション評価式のテスト");</js></method> </component> <component name="panel1" class="javax.swing.JPanel" instance="singleton" autoBinding="none"> <method name="add"> <arg>intput1</arg> </method> </component> <component name="intput1" class="javax.swing.JTextField" instance="prototype" autoBinding="none"> <arg><js>new java.text.SimpleDateFormat("yyyy'年'MM'月'dd'日'").format(new java.util.Date());</js></arg> </component> </components>
メソッドインジェクションで名前が無い(nameアトリビュートが無い)ものは式評価だけを行うのだ。式中の変数"self"はJavaの世界からバインドされたものであり、コンポーネント(この場合はJFrameである"mainFrame")への参照なので、JFrameのメソッドは自由に呼び出せることになる。
- 実行結果