1.Spring El使用方法
xml的使用方法:
<bean id="itemBean" class="com.yiibai.core.Item">
<property name="name" value="itemA" />
<property name="qty" value="10" />
</bean>
<bean id="customerBean" class="com.yiibai.core.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean>
注解的使用方法:
@Value("#{itemBean}")
private Item item;
@Value("#{itemBean.name}")
private String itemName;
2.Spring EL方法调用实例
如下,可通过调用bean的实例方法惊行注入
@Component("priceBean")
public class Price {
public double getSpecialPrice() {
return new Double(199.09);
}
}
可通过调用getSpecialPrice方法惊醒注入
@Value("#{'yiibai'.toUpperCase()}")
private String name;
@Value("#{priceBean.getSpecialPrice()}")
private double amount;
3.Spring EL运算符实例
1.关系运算符 – 等于 (==, eq), 不等于 (!=, ne), 小于 (<, lt), 小于或等于 (<= , le), 大于 (>, gt), 和大于或等于 (>=, ge).
2.逻辑运算符 – 且(and), 或(or), 非 (!).
3.数学运算符 – 加法(+), 减法 (-), 乘法 (*), 除法(/), 除模(%) 和指数幂 (^).
实例如下:
@Value("#{1 >= 1}") //true
private boolean testGreaterThanOrEqual;
//Logical operators , numberBean.no == 999
@Value("#{numberBean.no == 999 and numberBean.no < 900}") //false
private boolean testAnd;
@Value("#{1 + 1}") //2.0
private double testAdd;
@Value("#{'1' + '@' + '1'}") //1@1
private String testAddString;
4.Spring EL三目运算
@Value("#{itemBean.qtyOnHand < 100 ? true : false}")
private boolean warning;
5.Spring EL集合
private Map<String, String> map;
private List<String> list;
public Test() {
map = new HashMap<String, String>();
map.put("MapA", "This is MapA");
map.put("MapB", "This is MapB");
map.put("MapC", "This is MapC");
list = new ArrayList<String>();
list.add("List0");
list.add("List1");
list.add("List2");
}
应用:
@Value("#{testBean.map['MapA']}")
private String mapA;
@Value("#{testBean.list[0]}")
private String list;
6.Spring EL正则表达
@Value("#{'100' matches '\\d+' }")
private boolean isDigit;