25

When injecting any services, I have two choices :

Field injection:

 @Inject 
 private MyService myService;

or Constructor injection:

private MyService myService; 

@Inject
public ClassWhereIWantToInject(MyService mySerivce){
    this.myService = myService;
}

Why is Constructor injection better than Field injection?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Riadh
  • 1,088
  • 2
  • 12
  • 25

4 Answers4

19

I found only two disadvantages in the field injection.

  • Hard to inject mocks when the object is under test. (Can be resolved with @InjectMocks from Mockito)

  • Circle dependencies. If bean A depends on bean B and bean B needs bean A. If you have the constructor injection it easy to find it.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
dehasi
  • 2,644
  • 1
  • 19
  • 31
  • It's easy to find it, of course, but circular injection problems might only occur through the constructor variant. – Florian R. Klein Jun 01 '22 at 12:36
  • 1
    @FlorianR.Klein It's sort-of the other way round: With constructor injection, it's impossible to have a circular injection so you can't have problems; with field inject, it's possible that the injected object isn't fully initialized yet but the class may already use it (think lifecycle callbacks that happen during initialization). Cyclic dependencies are extremely nasty because by necessity, there is at least one object that is partially initialized (the cyclic link is still null), and this can give you very weird errors during startup that just vanish after init. – toolforger Aug 10 '22 at 15:45
9

Reading this excellent post (https://blog.marcnuri.com/field-injection-is-not-recommended/) we can find better explanations about why Field Injection is not a good choice.

It not allow us to make the Field immutable, using the final keyword.

It also is a step further away from SRP (Single Responsibility Principle), once the Class that has this field starts to have some responsibility on initialization timing of third-party classes.

PedroPK
  • 519
  • 6
  • 8
8

Do something like (I assume you are using spring-boot or something comparable for your CDI)

public class ClassWhereIWantToInject{

    private MyService myService; 

    @Inject
    public ClassWhereIWantToInject(MyService mySerivce){
        this.myService = myService;
    }
}

At this related question there are some valid arguments why to use injection via constructor instead of injection via field. It boils down to the advantage that you can use initialization via constructor also in non-CDI environment i.e. Unit Test, without the need to add more complex logic.

Community
  • 1
  • 1
hecko84
  • 1,224
  • 1
  • 16
  • 29
  • 1
    I'm using EJB-CDI. The question is which the best why for injection: Field or Constructor? The answer is [here](https://sonarqube.com/coding_rules#rule_key=squid%3AS3306) – Riadh Nov 22 '16 at 09:21
  • 1
    Haha, great I should have looked on the comments on your question before typing. I thought you want to avoid the warning, that's why I added the piece of code. – hecko84 Nov 22 '16 at 09:23
2

Field injection will be performed correctly if the class that contains this injected will be inject by the framework (spring/ejb/cdi), otherwise (the class will be instantiated by the caller using the new operator) it's really a NullPointerException waiting to happen. In this case, it is better to use constructor injection.

We can perform a reliable field injection, when the injection will be made in a class injected by the framework.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Riadh
  • 1,088
  • 2
  • 12
  • 25