9B3.SpEl实例
原创2022/7/14实战基础小于 1 分钟
9B3.SpEl实例
在Cacheable,DoubleKill的Aop场景中会用到 Spring Expression Language (SpEL)
9B3.1.Aop中使用
@Cacheable和@DoubleKill基于MethodBasedEvaluationContext存在以下内置变量
#root.method- 被Aop的method#root.target- method的执行对象#root.caches- 受影响的缓存(DK无)#root.methodName- method.getName#root.targetClass- target.getClass#root.args- method的所有参数#root.args[0],#p0or#a0,都为第1个参数,0-based#paraName- 以参数名使用,通过ParameterNameDiscoverer#result- 调用结果(DK无)
9B3.2.常用表达
特殊引用和格式
#this- refers to the current evaluation object#root- refers to the root context object@foo- lookup bean named fooT(xxx)- refers to type#{ ... }- expression templating
null检查及默认值 @Value("#{systemProperties['pop3.port'] ?: 25}")
null?:'Unknown'- name != null ? name : "Unknown"PlaceOfBirth?.City- PlaceOfBirth.City != null ? PlaceOfBirth.City : null
类stream的filter和map操作
Members.?[Nationality == 'Serbian']- Members.filterMembers.![placeOfBirth.city]- Members.map(it.placeOfBirth.city)
常规列表类和对象类导航
Members[0].Inventions[6]Officers['president'].PlaceOfBirth.City
字面量构造List
{1,2,3,4}-[1,2,3,4]的List<Integer>{{'a','b'},{'x','y'}}-[['a','b'],['x','y']]的List<List<String>>
