下边是关于SpeEL的解释及初步使用。
spirng 表达式语言:SpELSpring 表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言语法类似于EL:SpEL使用#{}作为定界符,所有在大框号中的字符将被认为是SpELSpEL为bean属性进行动态赋值提供了便利,通过SpEL可以实现 通过bean的id对bean进行引用, 调用方法以及引用对象中属性。 计算表达式值。 正则表达式的匹配。SpEL:字面量的表示: 整数:<property name="count" value="#{5}"/> 小数:<property name="frequency" value="#{89.4}"/> 科学计数法:<property name="capacity" value="1e4"/>String 可以使用单引号或者双引号作为字符串定界符,<property name="name" value="#{'Chuck'}"/>或<property name="name" value='#{"Chuck"}'/>Boolean <property name="enable" value="#{false}"> 引用Bea。属性和方法引用其他对象<!--通过value属性和SpEl配置之间的应用联系--><property name="prefix" value="#{prefixGenerator}"></property>引用其他对象的属性<!--通过value属性和SpEL配置suffix属性值为另一个Bean的suffix属性值--> <property name="suffix" value="#{sequenceGenerator2.suffix}"> <!--调用其他方法,还可以链试操作--><!--通过value属性和spel配置suffix属性值为另一个Bean的方法的返回值--> <property name="suffix" value="#{sequenceGenerator.toString()}"/> <!--方法的连缀--><property name="suffix" value="#{sequenceGenerator2.toString().toUpperCase()}">SpEL:支持绝大多数的算术运算符。SpEL:应用Bean,属性和方法。 调用静态方法或静态属性,通过T()调用一个类的静态方法,它将返回一个Class Object然后在调用相应的方法或者属性。 <property name="initValue" value="#{T(java.lang.Math).PI }">
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.spring.beans.autowires.spel.Address"> <!-- 使用spel为属性赋值(字面值) --> <property name="city" value="#{'BEIJING'}"></property> <property name="street" value="#{'WuDaoKou'}"></property> </bean> <bean id="car" class="com.spring.beans.autowires.spel.Car"> <property name="brand" value="Audi-1"></property> <property name="price" value="290000"></property> <!-- 使用SpeEL引用类的静态属性--> <property name="tyreperimeter" value="#{T(java.lang.Math).PI*80}"></property> </bean> <bean id="person" class="com.spring.beans.autowires.spel.Person"> <!-- 使用Spel来应用其他的Bean --> <property name="car" value="#{car}"></property> <!-- 使用address的city属性 --> <property name="city" value="#{address.city}"></property> <property name="info" value="#{car.price>=300000?'金领':'白领'}"></property> <property name="name" value="WEI"></property> </bean></beans>
新闻热点
疑难解答