SpringEL(主要用于缓存)

来源:互联网 发布:电脑弹古筝软件 编辑:程序博客网 时间:2024/05/17 23:35
什么是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注入简单值

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * @Value注解等同于XML配置中的<property/>标签,  
  5.      * SpringEL同样支持在XML<property/>中编写 
  6.      */  
  7.   
  8.     // 注入简单值,输出num为5  
  9.     @Value("#{5}")  
  10.     private Integer num;  
  11.   
  12.     // 注入ID为testConstant的Bean  
  13.     @Value("#{testConstant}")  
  14.     private TestConstant Constant;  
  15.   
  16.     // 注入ID为testConstant Bean中的STR常量/变量  
  17.     @Value("#{testConstant.STR}")  
  18.     private String str;  
  19.   
  20. }  


      使用SpringEL调用方法

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * TestConstant类中有两个方法重载, 
  5.      * 返回值为String类型 
  6.      */  
  7.   
  8.     // 调用无参方法  
  9.     @Value("#{testConstant.showProperty}")  
  10.     private String method1;  
  11.   
  12.       
  13.     // 有参接收字符串的方法  
  14.     @Value("#{testConstant.showProperty('Hello')}")  
  15.     private String method2;  
  16.   
  17.       
  18.     /* 
  19.      * 若然希望方法返回的String为大写 
  20.      */  
  21.     @Value("#{testConstant.showProperty().toUpperCase()}")  
  22.     private String method3;  
  23.   
  24.       
  25.     /* 
  26.      * 若使用method3这种方式,若然showProperty返回为null,  
  27.      * 将会抛出NullPointerException,可以使用以下方式避免 
  28.      */  
  29.     @Value("#{testConstant.showProperty()?.toUpperCase}")  
  30.     private String method4;  
  31.     /* 
  32.      * 使用?.符号代表若然左边的值为null,将不执行右边方法,  
  33.      * 读者可以灵活运用在其他场景,只要左边可能返回null, 
  34.      * 即可使用上面示例中的?. 
  35.      */  
  36. }  


      SpringEL调用静态类或常量

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 注入JDK中的工具类常量或调用工具类的方法 
  5.      */  
  6.   
  7.     // 获取Math的PI常量  
  8.     @Value("#{T(java.lang.Math).PI")  
  9.     private double pi;  
  10.   
  11.     // 调用random方法获取返回值  
  12.     @Value("#{T(java.lang.Math).random()}")  
  13.     private double ramdom;  
  14.   
  15.     // 获取文件路径符号  
  16.     @Value("#{T(java.io.File).separator}")  
  17.     private String separator;  
  18. }  


      SpringEL运算

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 使用SpringEL进行运算及逻辑操作 
  5.      */  
  6.   
  7.     // 拼接字符串  
  8.     @Value("#{testConstant.nickname + ' ' + testConstant.name}")  
  9.     private String concatString;  
  10.   
  11.     // 对数字类型进行运算,testConstant拥有num属性  
  12.     @Value("#{ 3 * T(java.lang.Math).PI + testConstant.num}")  
  13.     private double operation;  
  14.   
  15.     // 进行逻辑运算  
  16.     @Value("#{testConstant.num > 100 and testConstant.num <= 200}")  
  17.     private boolean logicOperation;  
  18.   
  19.     // 进行或非逻辑操作  
  20.     @Value("#{ not testConstant.num == 100 or testConstant.num <= 200}")  
  21.     private boolean logicOperation2;  
  22.   
  23.     // 使用三元运算符  
  24.     @Value("#{testConstant.num > 100 ? testConstant.num : testConstant.num + 100}")  
  25.     private Integer logicOperation3;  
  26. }  


      SpringEL使用正则表达式

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.     // 验证是否邮箱地址正则表达式  
  3.     @Value("#{testConstant.STR match '\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+'}")  
  4.     private boolean regularExpression;  
  5. }  


      SpringEL操作集合

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * TestConstant类中拥有名为testList的List变量, 和名为testMap的Map 
  5.      */  
  6.   
  7.     // 获取下标为0的元素  
  8.     @Value("#{testConstant.testList[0]}")  
  9.     private String str;  
  10.   
  11.     // 获取下标为0元素的大写形式  
  12.     @Value("#{testConstant.testList[0]?.toUpperCase()}")  
  13.     private String upperStr;  
  14.   
  15.     // 获取map中key为hello的value  
  16.     @Value("#{testConstant.testMap['hello']}")  
  17.     private String mapValue;  
  18.   
  19.     // 根据testList下标为0元素作为key获取testMap的value  
  20.     @Value("#{testConstant.testMap[testConstant.testList[0]]}")  
  21.     private String mapStrByTestList;  
  22. }  


      Spring操作外部Properties文件


      首先通过applicaContext.xml中<util:properties>增加properties文件

Xml代码  收藏代码
  1. <!-- 注意需要引入Spring的util schemea命名空间和注意id属性,id属性将在SpringEL中使用 -->  
  2.     <util:properties id="test" location="classpath:application.properties" />  

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     // 注意test为xml文件中声明的id  
  4.     @Value("#{test['jdbc.url']}")  
  5.     private String propertiesValue;  
  6. }  


      SpringEL查询筛选集合和投影

Java代码  收藏代码
  1. public class TestSpringEL {  
  2.   
  3.     /* 
  4.      * 声明City类,有population属性 testContants拥有名叫cityList的City类List集合 
  5.      */  
  6.   
  7.     // 过滤testConstant中cityList集合population属性大于1000的全部数据注入到本属性  
  8.     @Value("#{testConstant.cityList.?[population > 1000]}")  
  9.     private List<City> cityList;  
  10.   
  11.     // 过滤testConstant中cityList集合population属性等于1000的第一条数据注入到本属性  
  12.     @Value("#{testConstant.cityList.^[population == 1000]}")  
  13.     private City city;  
  14.   
  15.     // 过滤testConstant中cityList集合population属性小于1000的最后一条数据注入到本属性  
  16.     @Value("#{testConstant.cityList.$[population < 1000]}")  
  17.     private City city2;  
  18.   
  19.     /* 
  20.      * 首先为city增加name属性,代表城市的名称 
  21.      */  
  22.       
  23.     /* 
  24.      * 假如我们在过滤城市集合后只想保留城市的名称, 
  25.      * 可以使用如下方式进行投影 
  26.      */  
  27.     @Value("#{testConstant.cityList.?[population > 1000].![name]}")  
  28.     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,适当的技术方在适当的位置,才能更好的完成事情
原创粉丝点击