9B3.SpEl Sample
Original7/14/22PracticeKnowledgeLess than 1 minute
9B3.SpEl Sample
Spring Expression Language (SpEL) is used in Cacheable, DoubleKill's Aop scenario.
9B3.1.Use in Aop
@Cacheable and @DoubleKill have the following built-in variables based on the MethodBasedEvaluationContext
#root.method- the method being Aop#root.target- the method's execution target#root.caches- the affected cache (DK none)#root.methodName- method.getName#root.targetClass- target.getClass#root.args- all parameters of method#root.args[0],#p0or#a0, all for the 1st argument, 0-based#paraName- used as parameter name, via ParameterNameDiscoverer#result- the result of the call (DK none)
9B3.2.Common Use
Special references and formats
#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 check and default values @Value("#{systemProperties['pop3.port'] ?: 25}")
null?:'Unknown'- name != null ? name : "Unknown"PlaceOfBirth?.City- PlaceOfBirth.City != null ? PlaceOfBirth.City : null
stream like filter and map operations
Members.?[Nationality == 'Serbian']- Members.filterMembers.![placeOfBirth.city]- Members.map(it.placeOfBirth.city)
General list and object navigation
Members[0].Inventions[6]Officers['president'].PlaceOfBirth.City
Literal Constructed List
{1,2,3,4}-[1,2,3,4]ofList<Integer>{{'a','b'},{'x','y'}}-[['a','b'],['x','y']]ofList<List<String>>
