用Java注解注入一个变量

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

injecting a variable using Java annotation

问题

我已经创建了一个注解,我需要像在SLF4j日志变量中一样在JAVA中注入一个变量。以下是迄今为止我的代码...

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.TYPE)
  3. public @interface ClassLevelAnno {
  4. public String testVariable = "abc";
  5. }

而且我尝试在以下类中使用这个注解

  1. package test;
  2. @ClassLevelAnno
  3. public class UsingTestAnno {
  4. // 我需要直接使用ClassLevelAnno类中定义的testVariable,如下所示。
  5. public void testMethod() {
  6. System.out.println(testVariable);
  7. }
  8. }

就像在上面的类中一样,我需要直接打印变量。
为什么这个类级别的注解没有继承它的字段。

英文:

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 ...

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.TYPE)
  3. public @interface ClassLevelAnno {
  4. public String testVariable = "abc";
  5. }

And I am trying to use this annotation in the following class

package test;

  1. @ClassLevelAnno
  2. public class UsingTestAnno {
  3. // I need to use testVariable defined in ClassLevelAnno class directly like follow.
  4. public void testMethod() {
  5. System.out.println(testVariable);
  6. }
  7. }

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:

确定