`@Inject` 注解用于 Java 中的两个依赖类的两个构造函数。

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

@Inject Annotation of 2 constructors for 2 dependent classes in Java

问题

以下是翻译好的代码部分:

public class ClassA {

       private final String check;
       private final ClassB classB;

       //ClassA 构造函数

       @Inject

       public ClassA(String check, ClassB classB) {

         this.check = check + "A";
         this.classB = classB;

       }

       public String getCheck() {

             String check = classB.getCheck();
             print(check) ///////////////返回 checkA
       }

     }


public class ClassB {

      private final String check;

     //ClassB 构造函数

     @Inject

     public ClassB(String check) {

        this.check = check + "B";

     }

     public String getCheck() { 

          return check;
        }
      }

因此,在两个构造函数都使用了 @Inject 注解后,classB.getCheck(); 应该返回 checkB,但实际上返回的是 checkA。我认为这是因为 classB 构造函数并没有被执行,可能是由于 @Inject 注解导致的。但我不确定具体的原因。是否有任何建议或遗漏的地方?

英文:

Here is the code I am trying :

public class ClassA {

   private final String check;
   private final ClassB classB;

   //ClassA constructor

   @Inject

   public ClassA(String check, ClassB classB) {

     this.check = check + "A";
     this.classB = classB;

   }

   public String getCheck() {

         String check = classB.getCheck();
         print(check) ///////////////return checkA
   }

 }

public class ClassB {

  private final String check;

 //ClassB constructor

 @Inject

 public ClassB(String check) {

    this.check = check + "B";

 }

 public String getCheck() { 

      return check;
    }
  }

So, when @Inject is annotated for both the constructors, classB.getCheck(); should return checkB but it returns checkA. I assume this is because the classB constructor is not being executed due to @Inject annotations. But I am not sure of the exact reason. Any suggestion what I am missing?

huangapple
  • 本文由 发表于 2020年4月7日 13:57:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61073627.html
匿名

发表评论

匿名网友

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

确定