我正在使用@cacheable注释来缓存函数的结果.我有3个不同的缓存,每个缓存的关键是当前登录用户的用户ID与方法中的参数连接.在某个事件中,我想要驱逐所有具有以该特定用户ID开头的密钥的缓存条目.例如 :
@Cacheable(value = "testCache1", key = "'abcdef'")
Run Code Online (Sandbox Code Playgroud)
我想缓存evict注释是这样的:
@CacheEvict(value = "getSimilarVendors", condition = "key.startsWith('abc')")
Run Code Online (Sandbox Code Playgroud)
但是当我尝试实现它时,它给了我一个错误:
Property or field 'key' cannot be found on object of type'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public?
Run Code Online (Sandbox Code Playgroud)
实现这个的正确方法是什么?
注释如下:
@Pattern(regexp = "^[0-9]+$")
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
当我用@MyTestAnnotion注释一个字段时,即使该字段不包含所有数字,该呼叫也会顺利进行。
以下部分已由以下给出的答案解决。主要问题仍然存在。我尝试添加:
@Pattern(regexp = "^[0-9]+$")
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface MyTestAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
在我的@MyTestAnnotation上,它不会让我输入message属性,但是当我运行代码时,它给出了错误,指出未指定message属性。
如果我将@Pattern批注直接放在字段上,则其行为将符合预期。
我在这里想念什么吗?