spring-SpEL的使用
2018-01-26 23:29
162 查看
SPEL(Spring Expression Language)即Spring3中功能丰富强大的表达式语言,简称SpEL。SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式,存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL expression }。
通过SpEL可以实现:
1.通过bean的id对bean进行引用
2.调用方法以及引用对象的属性
3.计算表达式得值
4.正则表达式的匹配
下面我就分别对上面四种作用进行代码展示:
Address
Car
Person
beans-spel.xml
本人小白,如有错误,请指正。谢谢。
通过SpEL可以实现:
1.通过bean的id对bean进行引用
2.调用方法以及引用对象的属性
3.计算表达式得值
4.正则表达式的匹配
下面我就分别对上面四种作用进行代码展示:
Address
package com.yuehailin.spring.beans.spel; /** * Created by yuehailin on 2018/1/26. */ public class Address { private String city; private String street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address{" + "city='" + city + '\'' + ", street='" + street + '\'' + '}'; } }
Car
package com.yuehailin.spring.beans.spel; /** * Created by yuehailin on 2018/1/26. */ public class Car { private String brand; private double price; // 轮胎的周长 private double tyrePerimeter; public double getTyrePerimeter() { return tyrePerimeter; } public void setTyrePerimeter(double tyrePerimeter) { this.tyrePerimeter = tyrePerimeter; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + '}'; } public Car() { System.out.println("car"); } }
Person
package com.yuehailin.spring.beans.spel; /** * Created by yuehailin on 2018/1/26. */ public class Person { private String name; private Car car; // 引用address的city属性 private String city; // 根据car的price确定info:car的price>=300000为金领,否则为白领。 private String info; public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", car=" + car + ", city='" + city + '\'' + ", info='" + info + '\'' + '}'; } }
beans-spel.xml
<?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.yuehailin.spring.beans.spel.Address"> <property name="city" value="#{'BeiJing'}"></property> <property name="street" value="daxiangzi"></property> </bean> <bean id="car" class="com.yuehailin.spring.beans.spel.Car"> <property name="brand" value="baoma"></property> <property name="price" value="280000"></property> <!--使用spel引用类的静态属性--> <property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property> </bean> <bean id="person" class="com.yuehailin.spring.beans.spel.Person"> <!--使用spel来引用其他的bean--> <property name="car" value="#{car}"></property> <property name="city" value="#{address.city}"></property> <property name="info" value="#{car.price>300000 ? '金领':'白领'}"></property> <property name="name" value="岳海林"></property> </bean> </beans>
本人小白,如有错误,请指正。谢谢。
相关文章推荐
- Spring SpEL语言的使用
- Spring4.3x教程之二SpEL表达式的使用
- Spring 使用表达式进行装配 SpEL表达式
- spring 使用spel
- Spring使用SpEL表达式装配Bean
- Spring3.x--使用SpEL表达式装配
- Spring4 学习系列之——SpEl表达式的使用
- Spring的SpEL表达式标准简单使用
- spring_scope作用域和使用外部属性文件和spEL表达式
- Spring基于XML的DI-使用SPEL注入
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- Spring(2、Spring中Spel的使用)
- spring组件扫描<context:component-scan/>使用详解
- 在SSH框架中使用Spring的好处
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- Spring学习之使用XML配置AOP
- 使用spring提供的测试功能结合JUnit4进行单元测试
- 使用spring创建定时器
- SpringBoot是使用socket注解式