用Java注解注入一个变量

huangapple 未分类评论48阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月27日 12:08:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/63108578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定