spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException- Unexpected token.

  本篇文章为你整理了spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'()的详细内容,包含有 spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)',希望能帮助你了解 spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'。

  

@Cacheable(cacheNames = ENTERPRISE_CACHE_KEY, key = "#{T(com.emax.common.RestApiSignUtil).foo(#root.args[0])}")

 

  异常:

  org.springframework.expression.spel.SpelParseException: Expression [#{T(com.emax.common.RestApiSignUtil).foo(#root.args[0])}] @1: EL1043E: Unexpected token. Expected identifier but was lcurly({)

  异常stacktrace信息:

  

org.springframework.expression.spel.SpelParseException: Expression [#{T(com.emax.common.RestApiSignUtil).foo(#root.args[0])}] @1: EL1043E: Unexpected token. Expected identifier but was lcurly({)

 

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.internalException(InternalSpelExpressionParser.java:1044)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatToken(InternalSpelExpressionParser.java:926)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatFunctionOrVar(InternalSpelExpressionParser.java:423)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatStartNode(InternalSpelExpressionParser.java:512)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:351)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:345)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPowerIncDecExpression(InternalSpelExpressionParser.java:304)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatProductExpression(InternalSpelExpressionParser.java:282)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatSumExpression(InternalSpelExpressionParser.java:264)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatRelationalExpression(InternalSpelExpressionParser.java:218)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalAndExpression(InternalSpelExpressionParser.java:205)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalOrExpression(InternalSpelExpressionParser.java:192)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:153)

   at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:131)

   at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61)

   at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33)

   at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:52)

   at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:43)

   at org.springframework.context.expression.CachedExpressionEvaluator.getExpression(CachedExpressionEvaluator.java:88)

   at org.springframework.cache.interceptor.CacheOperationExpressionEvaluator.key(CacheOperationExpressionEvaluator.java:104)

   at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:778)

   at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:575)

   at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:518)

   at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:401)

   at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)

   at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)

   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)

   at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)

   at com.emax.MyService$$EnhancerBySpringCGLIB$$4de73e25.getEnterpriseByEntId( generated )

   at com.emax.MyServiceTest.getEnterpriseByEntId(MyServiceTest.java:21)

 

  View Code

  

  identifier、lcurly都是什么?一脸懵逼。
顺着stacktrace点进去看源码,才确定lcurly表示左大括号“{”。看来不能用“{”。下面是正确答案:

  

@Cacheable(cacheNames = ENTERPRISE_CACHE_KEY, key = "T(com.emax.common.RestApiSignUtil).foo(#root.args[0])")

 

  

  在SpEL中可以调用类的方法,足见SpEL的强大!

  
扩展:@Cacheable中SpEL的用法列举

  // 下面方式可以同时设置两个缓存; 取参数可以用#param形式
@Cacheable(cacheNames = {ENTERPRISE_CACHE_KEY, "test-another-key"}, key = "#enterpriseId eq 0 ? 0: #enterpriseId")
public Result baseEnterpriseByEntId(Long enterpriseId) {...}

  -------------------------------------------------

  // #root代表的是spring-context-***.jar中的CacheExpressionRootObject,该类里有Object[] args属性及其getter方法。
@Cacheable(cacheNames = ENTERPRISE_CACHE_KEY, key = "#root.args[0] =0? 0 : #root.args[0]")
public Result getEnterpriseByEntId1(Long enterpriseId) {...}

  -------------------------------------------------

  // SpEL中调用外部类函数
@Cacheable(cacheNames = ENTERPRISE_CACHE_KEY, key = "T(com.emax.common.MyUtil).foo(#root.args[0])")
public Result getEnterpriseByEntId(Long enterpriseId) {...}

  // SpEL中调用外部类函数--- 设置缓存key常用的套路:当缓存key包含一个对象时,则做md5
@Cacheable(cacheNames = ENTERPRISE_CACHE_KEY, key = "T(com.emax.common.Md5Util).sign(T(com.alibaba.fastjson.JSON).toJSONString(#root.args[0]))")
public Result getEnterpriseByEntId2(UserAccountVo userAccountVo, Long enterpriseId) {...}

  
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自,转载请注明原文链接:https:///buguge/p/16782597.html

  以上就是spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'()的详细内容,想要了解更多 spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: