英文:
injecting a variable using Java annotation
问题
我已经创建了一个注解,我需要像在SLF4j日志变量中一样在JAVA中注入一个变量。以下是迄今为止我的代码...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassLevelAnno {
public String testVariable = "abc";
}
而且我尝试在以下类中使用这个注解
package test;
@ClassLevelAnno
public class UsingTestAnno {
// 我需要直接使用ClassLevelAnno类中定义的testVariable,如下所示。
public void testMethod() {
System.out.println(testVariable);
}
}
就像在上面的类中一样,我需要直接打印变量。
为什么这个类级别的注解没有继承它的字段。
英文:
I have created an annotation and I need to inject a variable (like in SLF4j log variable) using JAVA. Below is my code so far ...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassLevelAnno {
public String testVariable = "abc";
}
And I am trying to use this annotation in the following class
package test;
@ClassLevelAnno
public class UsingTestAnno {
// I need to use testVariable defined in ClassLevelAnno class directly like follow.
public void testMethod() {
System.out.println(testVariable);
}
}
As in the above class, I need to print the variable directly.
Why this class level annotation didn't inherit its fields.
专注分享java语言的经验与见解,让所有开发者获益!
评论