我正在尝试将 Redis 与 Spring 一起使用@Cacheable
但需要根据 Spring Boot 样式应用程序属性有条件地打开或关闭缓存。我的第一次尝试似乎不起作用。
application.properties 文件:
auth.token-cache-enabled=false
属性类:
@Component
@ConfigurationProperties(prefix = "auth")
public class AuthProperties {
public boolean tokenCacheEnabled;
...
}
服务方法注释:
@Cacheable(key = "#token", condition = "@authProperties.tokenCacheEnabled()")
结果是:
org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'authProperties' at org.springframework.expression.spel.ast.BeanReference.getValueInternal(BeanReference.java:48)
有谁知道问题是什么或者是否有其他方法可以实现这一目标?
我找到了一种方法可以在我的情况下完成这项工作,但我还发现了一个我认为是同一问题的错误票:https://jira.spring.io/browse/SPR-13812 https://jira.spring.io/browse/SPR-13812
我的解决方法是@Inject
my AuthProperties
进入包含我要缓存的方法的服务。接下来,我将该方法的缓存条件更改为:
@Cacheable(key = "#token", condition = "#root.target.authProperties.tokenCacheEnabled")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)