什么是SpringEL? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中,更可以调用JDK中提供的静态常量,获取外部Properties文件中的的配置
为什么要使用SpringEL? 我们平常通过配置文件或Annotaton注入的Bean,其实都可以称为静态性注入,试想一下,若然我Bean A中有变量A,它的值需要根据Bean B的B变量为参考,在这场景下静态注入就对这样的处理显得非常无力,而Spring3增加的SpringEL就可以完全满足这种需求,而且还可以对不同Bean的字段进行计算再进行赋值,功能非常强大
如何使用SpringEL? SpringEL从名字来看就能看出,和EL是有点关系的,SpringEL的使用和EL表达式的使用非常相似,EL表达式在JSP页面更方便的获取后台中的值,而SpringEL就是为了更方便获取Spring容器中的Bean的值,EL使用${},而SpringEL使用#{}进行表达式的声明 使用SpringEL注入简单值
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{5}")
-
-
private Integer num;
-
-
-
-
-
@Value("#{testConstant}")
-
-
private TestConstant Constant;
-
-
-
-
-
@Value("#{}")
-
-
private String str;
-
- }
使用SpringEL调用方法
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{}")
-
-
private String method1;
-
-
-
-
-
-
-
@Value("#{('Hello')}")
-
-
private String method2;
-
-
-
-
-
-
-
-
-
-
-
@Value("#{().toUpperCase()}")
-
-
private String method3;
-
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{()?.toUpperCase}")
-
-
private String method4;
-
-
-
-
-
-
-
-
-
-
- }
SpringEL调用静态类或常量
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{T().PI")
-
-
private double pi;
-
-
-
-
-
@Value("#{T().random()}")
-
-
private double ramdom;
-
-
-
-
-
@Value("#{T().separator}")
-
-
private String separator;
- }
SpringEL运算
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{ + ' ' + }")
-
-
private String concatString;
-
-
-
-
-
@Value("#{ 3 * T().PI + }")
-
-
private double operation;
-
-
-
-
-
@Value("#{ > 100 and <= 200}")
-
-
private boolean logicOperation;
-
-
-
-
-
@Value("#{ not == 100 or <= 200}")
-
-
private boolean logicOperation2;
-
-
-
-
-
@Value("#{ > 100 ? : + 100}")
-
-
private Integer logicOperation3;
- }
SpringEL使用正则表达式
-
public class TestSpringEL {
-
-
-
-
@Value("#{ match '\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+'}")
-
-
private boolean regularExpression;
- }
SpringEL操作集合
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{[0]}")
-
-
private String str;
-
-
-
-
-
@Value("#{[0]?.toUpperCase()}")
-
-
private String upperStr;
-
-
-
-
-
@Value("#{['hello']}")
-
-
private String mapValue;
-
-
-
-
-
@Value("#{[[0]]}")
-
-
private String mapStrByTestList;
- }
Spring操作外部Properties文件
首先通过中<util:properties>增加properties文件
-
-
-
<util:properties id="test"location="classpath:"/>
-
public class TestSpringEL {
-
-
-
-
-
@Value("#{test['']}")
-
-
private String propertiesValue;
- }
SpringEL查询筛选集合和投影
-
public class TestSpringEL {
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{.?[population > 1000]}")
-
-
private List<City> cityList;
-
-
-
-
-
@Value("#{.^[population == 1000]}")
-
-
private City city;
-
-
-
-
-
@Value("#{.$[population < 1000]}")
-
-
private City city2;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@Value("#{.?[population > 1000].![name]}")
-
-
private List<String> cityName;
优点:SpringEL功能非常强大,在Annotation的方式开发时可能感觉并不强烈,因为可以直接编写到源代码来实现SpringEL的功能,但若然是在XML文件中进行配置,SpringEL可以弥补XML静态注入的不足,从而实现更强大的注入 缺点:SpringEL在使用时仅仅是一个字符串,不易于排错与测试,也没有IDE检查我们的语法,当出现错误时较难检测
笔者实际应用:笔者开发的项目当中比较频繁的使用SpringEL,例如通过SpringEL获取外部properties中的值,又或者项目当中的数据字典亦是使用SpringEL的一个场景,我们抽象出一个Param类的集合,通过SpringEL集合筛选和投影获取我们想要的字段参数添加到我们的程序逻辑当中(笔者项目中的Spring Security亦使用SpringEL,但本文章不加以叙述)
总结:Spring3.0让人为之惊艳的非SpringEL莫属,为我们的注入提供了另一种强大的形式,传统注入能做到的事情,和做不到的事情,SpringEL一概能完成,但在项目当中并不适宜大量使用SpringEL,适当的技术方在适当的位置,才能更好的完成事情