SPEL -------- 使用表达式装配bean属性:
SPEL使用#{…}作为定界符:
SpEL:
1、使用bean的id来引用bean;
2、调用方法和访问对象的属性;
3、对值进行算术、关系和逻辑运算;
4、正则表达式匹配;
5、集合操作;
例:
<!-- 引用类:kenny的song方法 -->
<bean id="car" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="#{kenny.song}" />
</bean>
以上一段代码是通过ID找到kenny bean,然后调用kenny的song方法;
相当于执行下面代码:
Instrumentalist car = new Instrumentalist();
car.setSong(kenny.getSong());
例:
<property name="song" value="#{songSelector.selectSong()}" />
以上一段代码是为property注入一个selectSong方法;
例:
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}" />
以上一段代码:避免selectSong()抛出空异常
例:
<!-- 数字表达式 -->
<property name="adjustedAmount" value="#{counter.total + 42}"/>
<property name="adjustedAmount" value="#{counter.total - 20}"/>
<property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/>
<property name="average" value="#{counter.total / counter.count}"/>
<property name="remainder" value="#{counter.total % counter.count}"/>
<property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>