SpringEL表达式的使用 SpringBoot( 二 )


向controller中注入配置类 , 然后访问接口测试结果如下
{ "no": 1432516744, "num": 1001, "name": "el", "skill": "java", "listLanguage02": ["java","spring","mysql","linux" ], "strLanguage02": ["java","spring","mysql","linux" ]}
三、SpringEL-基础使用1、使用SpEL注入简单值和普通EL注入使用基本一致
2、SpEl注入map

  • 配置文件中需要使用双引号括起来 , 否则将会注入失败 , key为单引号
# SpElspEl:mapInject: "{'name': 'SpEl', 'website': 'http://www.codeocord.com'}"
java类中先使用${spEl.mapInject}注入字符串值 , #{}会解析字符串的值转为map
@Value("#{${spEl.mapInject}}")private Map<String, String> mapInject;
3、SpEl注入list
  • 除了可以通过EL注入listI外 , 也可以使用#{${}.split('分隔符')}的方式注入List
  • 配置文件中例如使用#分隔
spEl:listInject: "44#11#99#100"
java类中先使用${spEl.listInject}注入字符串值 , 内容使用单引号括起来 , 然后对字符串使用split方法分隔
提示:避免为空情况 , 可以给一个默认值空串
@Value("#{'${spEl.listInject:}'.split('#')}") private List<String> listInject;
4、动态注入
上述注入都是静态注入 , 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@Datapublic class SpElConstant {private String name = "SpElConstant-name";private String nickname = "tianxin";private int num = 100;private List<String> product = new ArrayList<String>() {{add("huaweiMate30Pro");add("xiaomi10x5g");}};private Map<String, String> productMap = new HashMap<String, String>() {{put("huaweiMate30Pro", "5999");put("xiaomi10x5g", "4999");}};private List<City> cityList = new ArrayList<City>() {{add(new City("深圳", 1000L));add(new City("杭州", 2000L));add(new City("贵阳", 900L));}};public String showProperty() {return "showProperty-无参数";}public String showProperty(String name) {return "showProperty-" + name;}@Data@AllArgsConstructorstatic class City {private String name;private long 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@Componentpublic class SpElConfig {/// 不支持直接注入配置文件值/*@Value("#{com.codecoord.el.num}")private Integer num;*//*** 对象注入*/@Value("#{spElConstant}")private SpElConstant spElConstant;/*** 注入ID为spElConstant Bean中的STR常量/变量*/@Value("#{spElConstant.name}")private String name;/*** 调用无参方法*/@Value("#{spElConstant.showProperty()}")private String method1;/*** 有参接收字符串的方法*/@Value("#{spElConstant.showProperty('Hell SpringEL')}")private String method2;/*** 方法返回的String为大写*/@Value("#{spElConstant.showProperty().toUpperCase()}")private String method3;/*** 若使用method3这种方式 , 若果showProperty返回为null* 将会抛出NullPointerException,可以使用以下方式避免* 使用?.符号代表若然左边的值为null , 将不执行右边方法*/@Value("#{spElConstant.showProperty()?.toUpperCase()}")private String method4;/*** 注入math常量*/@Value("#{T(java.lang.Math).PI}")private double pi;/*** 用random方法获取返回值*/@Value("#{T(java.lang.Math).random()}")private double random;/*** 获取文件路径符号*/@Value("#{T(java.io.File).separator}")private String separator;/*** 拼接字符串*/@Value("#{spElConstant.nickname + ' ' + spElConstant.name}")private String concatString;/*** 对数字类型进行运算,spElConstant拥有num属性*/@Value("#{3 * T(java.lang.Math).PI + spElConstant.num}")private double operation;/*** 进行逻辑运算*/@Value("#{spElConstant.num > 100 and spElConstant.num <= 200}")private boolean logicOperation;/*** 进行或非逻辑操作*/@Value("#{not (spElConstant.num == 100) or spElConstant.num <= 200}")private boolean logicOperation2;/*** 使用三元运算符*/@Value("#{spElConstant.num > 100 ? spElConstant.num : spElConstant.num + 100}")private Integer logicOperation3;/*** 获取下标为0的元素*/@Value("#{spElConstant.product[0]}")private String str;/*** 获取下标为0元素的大写形式*/@Value("#{spElConstant.product[0]?.toUpperCase()}")private String upperStr;/*** 获取map中key为hello的value*/@Value("#{spElConstant.productMap['hello']}")private String mapValue;/*** 根据product下标为0元素作为key获取testMap的value*/@Value("#{spElConstant.productMap[spElConstant.product[0]]}")private String mapStrByproduct;/*** 注入人口大于等于1000人口的城市*/@Value("#{spElConstant.cityList.?[population >= 1000]}")private List<SpElConstant.City> cityList;/*** 注入人口等于900人口的城市*/@Value("#{spElConstant.cityList.?[population == 900]}")private SpElConstant.City city;/*** 注入人口大于等于1000人口的城市 , 且只保留城市名称*/@Value("#{spElConstant.cityList.?[population >= 1000].![name]}")private List<String> cityName;}