Spring ELメソッドの呼び出し例
Spring式言語(SpEL)を使用すると、開発者は式を使用してメソッドを実行し、メソッドの戻り値をプロパティ、いわゆる「SpEL method invocation」に挿入できます。
注釈のSpring EL
@Valueアノテーションを使用してSpringELメソッドを呼び出す方法を参照してください。
package com.example.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{'example'.toUpperCase()}") private String name; @Value("#{priceBean.getSpecialPrice()}") private double amount; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } @Override public String toString() { return "Customer [name=" + name + ", amount=" + amount + "]"; } }
package com.example.core; import org.springframework.stereotype.Component; @Component("priceBean") public class Price { public double getSpecialPrice() { return new Double(99.99); } }
出力
Customer [name=MKYONG, amount=99.99]
説明
stringリテラルで ‘toUpperCase()
‘メソッドを呼び出します。
@Value("#{'example'.toUpperCase()}") private String name;
Bean ‘priceBean‘で ‘getSpecialPrice()
‘メソッドを呼び出します。
@Value("#{priceBean.getSpecialPrice()}") private double amount;
XMLのSpring EL
これは、Bean定義XMLファイルの同等のバージョンです。
出力
Customer [name=MKYONG, amount=99.99]
ソースコードをダウンロード
ダウンロード–Spring3-EL-Method-Invocation-Example.zip(6 KB)