OGNL
diconファイルではOGNL式(Object-Graph-Navigation Language)が使用可能です。
1. リテラル
3. 配列、マップ参照
4. 静的プロパティ、メソッド
5. オブジェクト生成
などなど
app.dicon |
<components>
<include path="convention.dicon"/>
<include path="aop.dicon"/>
<initMethod name="add">
<arg>"message1"</arg>
</initMethod>
<initMethod name="add">
<arg>"message2"</arg>
</initMethod>
<initMethod name="add">
<arg>"message3"</arg>
</initMethod>
</component>
<!-- コンポーネント定義 -->
<component name="testOGNL" class="TestOGNL" instance="singleton" autoBinding="property">
<property name="charValue">'c'</property>
<!-- リテラル -->
<property name="intValue">123456</property>
<property name="longValue">100L</property>
<property name="doubleValue">0.01D</property>
<property name="floatValue">10.50F</property>
<property name="stringValue">"out put string."</property>
<property name="boolValue">true</property>
<property name="bigIntValue">100000H</property>
<property name="bigDecValue">200.00B</property>
<!-- <property name="bigDecValue">コンポーネント名.プロパティ名</property> -->
<property name="listSize">list.size()</property>
<!-- staticメソッド、staticプロパティ-->
<property name="staticProperty">@java.lang.String@class</property>
<property name="staticMethod">@java.lang.Math@max(5,6)</property>
<!-- オブジェクト生成 -->
<!-- list生成 -->
<property name="list">{"list1", "list2"}</property>
<!-- map生成 -->
<property name="map">#{"key1":1, "key2":2, "key3":3}</property>
</component>
</components>
|
TestOGNL.java |
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
public class TestOGNL {
// リテラル
public char charValue;
public int intValue;
public long longValue;
public double doubleValue;
public float floatValue;
public String stringValue;
public boolean boolValue;
public BigInteger bigIntValue;
public BigDecimal bigDecValue;
// メソッド呼び出し
public int listSize;
// staticプロパティ、メソッド
public String staticProperty;
public String staticMethod;
// オブジェクト生成
public List<String> list;
public Map<String, Integer> map;
public String getFieldList() {
StringBuilder sb = new StringBuilder();
sb.append("リテラル結果").append("\r\n");
sb.append("---------------------------------------------").append("\r\n");
sb.append("charValue : ").append(charValue).append("\r\n");
sb.append("intValue : ").append(intValue).append("\r\n");
sb.append("longValue : ").append(longValue).append("\r\n");
sb.append("doubleValue : ").append(doubleValue).append("\r\n");
sb.append("floatValue : ").append(floatValue).append("\r\n");
sb.append("stringValue : ").append(stringValue).append("\r\n");
sb.append("boolValue : ").append(boolValue).append("\r\n");
sb.append("bigIntValue : ").append(bigIntValue).append("\r\n");
sb.append("bigDecValue : ").append(bigDecValue).append("\r\n");
sb.append("\r\n");
sb.append("---------------------------------------------").append("\r\n");
sb.append("listSize : ").append(listSize).append("\r\n");
sb.append("\r\n");
sb.append("staticプロパティ、メソッド呼び出し結果").append("\r\n");
sb.append("---------------------------------------------").append("\r\n");
sb.append("staticProperty : ").append(staticProperty).append("\r\n");
sb.append("staticMethod : ").append(staticMethod).append("\r\n");
sb.append("\r\n");
sb.append("オブジェクト生成").append("\r\n");
sb.append("---------------------------------------------").append("\r\n");
for(String msg : list) {
sb.append("生成したLISTの要素 : " + msg).append("\r\n");
}
for(Map.Entry<String, Integer> e : map.entrySet()) {
sb.append("生成したMAPのVALUE : " + e.getValue()).append("\r\n");
}
return sb.toString();
}
}
|
TestClass.java |
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
public class TestClass {
public static void main(String[] args) {
// S2Container初期化
SingletonS2ContainerFactory.init();
// コンポーネント取得
TestOGNL testOGNL = SingletonS2Container.getComponent("testOGNL");
System.out.println(testOGNL.getFieldList());
// S2Container破棄
SingletonS2ContainerFactory.destroy();
}
}
|
出力メッセージ |
リテラル結果
---------------------------------------------
charValue : c
intValue : 123456
longValue : 100
doubleValue : 0.01
floatValue : 10.5
stringValue : out put string.
boolValue : true
bigIntValue : 100000
bigDecValue : 200.00
---------------------------------------------
listSize : 3
staticプロパティ、メソッド呼び出し結果
---------------------------------------------
staticProperty : class java.lang.String
staticMethod : 6
オブジェクト生成
---------------------------------------------
生成したLISTの要素 : list1
生成したLISTの要素 : list2
生成したMAPのVALUE : 1
生成したMAPのVALUE : 2
生成したMAPのVALUE : 3
|