0

I have a doubt to use autowire annotation in different scenarios...

public Class A {
  @Autowired
  public B b;

  public void print() {
    System.out.println("the value is " + b.getValue());
  }

}

Example 2:

public Class A {
  public B b;

  @Autowired
  Public A(B b){}

  public void print() {
    System.out.println("the value is " + b.getValue());
  }
}
Rumid
  • 1,627
  • 2
  • 21
  • 39
  • Thanks in advance.. what is the dfifference in these two use cases.. – pauljeyasingh Mar 04 '17 at 17:24
  • 1
    What's your question? The first one uses field injection. The second one uses constructor injection. Read the documentation. Also, none of your code is valid Java code, and is indented. Take some care before pressing the submit button. – JB Nizet Mar 04 '17 at 17:25
  • 1
    http://coders-kitchen.com/2015/01/05/dependency-injection-field-vs-constructor-vs-method/ read this. – Tahir Hussain Mir Mar 04 '17 at 17:33
  • Thankyou JB Nizet.. can you refer some documents for detailed explanation on this – pauljeyasingh Mar 04 '17 at 18:27

1 Answers1

0

The first method to autowire objects is called field-injection and the second one is called constructor-injection. There are many posts on SO about these two topics! E.g. What exactly is Field Injection and how to avoid it? This should answer all of your questions.

Community
  • 1
  • 1
T. Jung
  • 3,327
  • 3
  • 11
  • 19