有些事情真的很奇怪。 我在注释值中使用静态最终字符串。
class Constants {
static final String myConstant = "ting tong"
}
class Service {
@CacheEvict(cacheNames = Constants.myConstant)
void doSomethingNice() {
}
}
但是,我无法编译它。
这是错误信息
Attribute 'myConstant' should have type 'java.lang.String'; but found type 'java.lang.Object' in @org.springframework.cache.annotation.CacheEvict
Expected 'Constants.getMyConstant()' to be an inline constant of type java.lang.String in @org.springframework.cache.annotation.CacheEvict
可能是什么问题?这在 Java 中完美运行
最佳答案
一般来说,将常量保存在接口(interface)下而不是类下是一种很好的做法。
interface Constants {
public static final String myConstant = "ting tong"
}
另一个好的做法是将常量定义为 static final 而不仅仅是 final,因为为每个常量只创建一个实例会更有效。
https://stackoverflow.com/questions/40038054/