我发现在 Java 中,当我对这样的方法使用注释时,这是不可能的。它给出“属性必须是常量”
private static final String CONSTANT = MyClass.class.getCanonicalName();
@Timed(CONSTANT) //Attribute value must be constant
@CircuitBreaker(name = CONSTANT) //Attribute value must be constant
public String something( String something ) {
强调一下,我在这里使用 @Timed 和 @CircuitBreaker 作为示例。许多其他注释也会产生完全相同的问题。
如何用Java实现呢?我的意思是,我不想像这样对这里的值进行硬编码。
@Timed("MyClass") @CircuitBreaker(name = "MyClass")
最佳答案
private static final String CONSTANT = MyClass.class.getCanonicalName();
这些是修饰符:private static final
这是数据类型/对象类型:字符串
这是常量/变量/对象的名称:CONSTANT
这是发起者:=
这是属性:MyClass.class.getCanonicalName();
您的属性不是常量,因此您无法启动常量CONSTANT
。
关于Java注解,属性值必须是常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64512253/