一、SpringEL-基础介绍
- 什么是SpringEL(SpEL)?
- Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式
- SpringEL可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中
- SpringEL可以调用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使用#{}进行表达式的声明
- 两者主要区别
- $是去找外部配置的参数,将值赋过来
- #是SpEL表达式,去寻找对应变量的内容
- 也可以直接使用@value(“常量”)注入不使用EL,这样写法与直接赋值等价
- 如果是在Spring中使用可以使用**@PropertySource(“classpath:my.properties”)**加载对应配置文件
二、EL表达式-基础使用
com:codecoord:el:num:1001name: ellanguage:- java- spring- mysql- linuxlanguage02: java,spring,mysql,linux
- 使用EL注入简单值
@Value("1432516744")private Integer no;
- 注入配置文件属性值
@Value("${com.codecoord.el.num}")private Integer num;@Value("${com.codecoord.el.name}")private String name;
- 注入默认值
@Value("${com.codecoord.el.skill:java}")private String skill;
- 注入列表
- 不支持直接配置文件中数组语法格式注入列表
- 可以识别使用逗号,分隔的配置,spring默认以,分隔
@Value("${com.codecoord.el.language}")private List<String> listLanguage;@Value("${com.codecoord.el.language}")private String[] strLanguage;
@Value("${com.codecoord.el.language02}")private List<String> listLanguage02;@Value("${com.codecoord.el.language02}")private String[] strLanguage02;
- 完整参考如下
server:port:8888com:codecoord:el:num:1001name: ellanguage:- java- spring- mysql- linuxlanguage02: java,spring,mysql,linux
import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.util.List;@Data@ComponentpublicclassElConfig{@Value("1432516744")private Integer no;@Value("${com.codecoord.el.num}")private Integer num;@Value("${com.codecoord.el.name}")private String name;@Value("${com.codecoord.el.skill:java}")private String skill;@Value("${com.codecoord.el.language02}")private List<String> listLanguage02;@Value("${com.codecoord.el.language02}")private String[] strLanguage02;}
- 向controller中注入配置类,然后访问接口测试结果如下
{"no":1432516744,"num":1001,"name":"el","skill":"java","listLanguage02":["java","spring","mysql","linux"],"strLanguage02":["java","spring","mysql","linux"]}
三、SpringEL-基础使用
- 使用SpEL注入简单值和普通EL注入使用基本一致
- SpEl注入map
- 配置文件中需要使用双引号括起来,否则将会注入失败,key为单引号
spEl:mapInject:"{'name': 'SpEl', 'website': 'http://www.codeocord.com'}"
- java类中先使用${spEl.mapInject}注入字符串值,#{}会解析字符串的值转为map
@Value("#{${spEl.mapInject}}")private Map<String, String> mapInject;
- SpEl注入list
- 除了可以通过EL注入listI外,也可以使用#{${}.split(‘分隔符’)}的方式注入List
- 配置文件中例如使用#分隔
spEl:listInject:"44#11#99#100"
- java类中先使用${spEl.listInject}注入字符串值,内容使用单引号括起来,然后对字符串使用split方法分隔
- 提示:避免为空情况,可以给一个默认值空串
@Value("#{'${spEl.listInject:}'.split('#')}")private List<String> listInject;
- 动态注入
- 上述注入都是静态注入,SpEl支持从Spring容器中注入信息,称为动态注入。动态注入类如下
import lombok.AllArgsConstructor;import lombok.Data;import org.springframework.stereotype.Component;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;@Component@DatapublicclassSpElConstant{private String name="SpElConstant-name";private String nickname="tianxin";privateint num=100;private List<String> product=newArrayList<String>(){{add("huaweiMate30Pro");add("xiaomi10x5g");}};private Map<String, String> productMap=newHashMap<String, String>(){{put("huaweiMate30Pro","5999");put("xiaomi10x5g","4999");}};private List<City> cityList=newArrayList<City>(){{add(newCity("深圳",1000L));add(newCity("杭州",2000L));add(newCity("贵阳",900L));}};public StringshowProperty(){return"showProperty-无参数";}public StringshowProperty(String name){return"showProperty-"+ name;}@Data@AllArgsConstructorstaticclassCity{private String name;privatelong population;}}
- SpEl支持和不支持操作
- 支持动态注入实例,类似于对象自动注入
- SPL不支持直接注入配置文件中的配置
- 支持调用静态和实例方法
- 静态方法:@Value("#{T(package.ClassName).ConstFieldName")
- 支持调用静态类或常量
- 支持运算符运算
- 支持操作集合
- 支持查询筛选集合和投影
- 注入完整操作如下
import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.util.List;import java.util.Map;@Data@ComponentpublicclassSpElConfig{@Value("#{spElConstant}")private SpElConstant spElConstant;@Value("#{spElConstant.name}")private String name;@Value("#{spElConstant.showProperty()}")private String method1;@Value("#{spElConstant.showProperty('Hell SpringEL')}")private String method2;@Value("#{spElConstant.showProperty().toUpperCase()}")private String method3;@Value("#{spElConstant.showProperty()?.toUpperCase()}")private String method4;@Value("#{T(java.lang.Math).PI}")privatedouble pi;@Value("#{T(java.lang.Math).random()}")privatedouble random;@Value("#{T(java.io.File).separator}")private String separator;@Value("#{spElConstant.nickname + ' ' + spElConstant.name}")private String concatString;@Value("#{3 * T(java.lang.Math).PI + spElConstant.num}")privatedouble operation;@Value("#{spElConstant.num > 100 and spElConstant.num <= 200}")privateboolean logicOperation;@Value("#{not (spElConstant.num == 100) or spElConstant.num <= 200}")privateboolean logicOperation2;@Value("#{spElConstant.num > 100 ? spElConstant.num : spElConstant.num + 100}")private Integer logicOperation3;@Value("#{spElConstant.product[0]}")private String str;@Value("#{spElConstant.product[0]?.toUpperCase()}")private String upperStr;@Value("#{spElConstant.productMap['hello']}")private String mapValue;@Value("#{spElConstant.productMap[spElConstant.product[0]]}")private String mapStrByproduct;@Value("#{spElConstant.cityList.?[population >= 1000]}")private List<SpElConstant.City> cityList;@Value("#{spElConstant.cityList.?[population == 900]}")private SpElConstant.City city;@Value("#{spElConstant.cityList.?[population >= 1000].![name]}")private List<String> cityName;}
- 注入结果
{"spElConstant":{"name":"SpElConstant-name","nickname":"tianxin","num":100,"product":["huaweiMate30Pro","xiaomi10x5g"],"productMap":{"xiaomi10x5g":"4999","huaweiMate30Pro":"5999"},"cityList":[{"name":"深圳","population":1000},{"name":"杭州","population":2000},{"name":"贵阳","population":900}]},"name":"SpElConstant-name","method1":"showProperty-无参数","method2":"showProperty-Hell SpringEL","method3":"SHOWPROPERTY-无参数","method4":"SHOWPROPERTY-无参数","pi":3.141592653589793,"random":0.19997238292235787,"separator":"\\","concatString":"tianxin SpElConstant-name","operation":109.42477796076938,"logicOperation":false,"logicOperation2":true,"logicOperation3":200,"str":"huaweiMate30Pro","upperStr":"HUAWEIMATE30PRO","mapValue":null,"mapStrByproduct":"5999","cityList":[{"name":"深圳","population":1000},{"name":"杭州","population":2000}],"city":{"name":"贵阳","population":900},"cityName":["深圳","杭州"]}
- Spring操作外部Properties文件
<!-- 首先通过applicaContext.xml中<util:properties>增加properties文件--><!-- 注意需要引入Spring的util schemea命名空间和注意id属性,id属性将在SpringEL中使用--><util:properties id="db" location="classpath:application.properties"/>
publicclassTestSpringEL{@Value("#{db['jdbc.url']}")private String propertiesValue;}
- SpringEL在使用时仅仅是一个字符串,不易于排错与测试,也没有IDE检查我们的语法,当出现错误时较难检测,复杂的表达式不建议通过SpringEL方式注入。在非必要情况下,不推荐使用SpEl的复杂注入,清晰可读的代码更为重要且有利于排查问题
四、属性自动注入
- 上述都是通过指定字段进行注入,可以通过@ConfigurationProperties指定前缀进行自动注入
- org.springframework.boot.context.properties.ConfigurationProperties
- 配置类
user:id: ${random.uuid}name: autowireaddress: unknownwebsite: www.codecoord.comage: ${random.int}
- 自动属性注入类
- 通过prefix指定前端为user,然后将会把user.后的类型按照名称进行注入
- 注意必须要提供setter方法
import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix="user")@DatapublicclassUserConfig{private String id;private String name;private String address;private String website;private Integer age;}
- 可以通过@EnableConfigurationProperties(value = UserConfig.class)将UserConfig再次强制注入,问题出现在如果UserConfig为第三方jar包内的配置类,则可能出现属性没有注入情况,所以可以指定注入