Dependency Injection – Field vs Constructor vs Method


today I would like to discuss in short different ways of injecting dependencies into your classes.

In general you have the following three options for injection

  • directly into fields/attributes
  • via explicit setter methods
  • via explicit constructor parameters

Field injection

This type of injection instruments some kind of reflection mechanism for injecting the required dependencies into the class.

While this injection type has the benefit, that it removes clutter code like setter methods or constructor parameters, it has the drawback that these dependencies are invisible. If you look at the class from the outside, you will only see the public methods and may be the constructor.

Even if this gives you a very clear idea what services a class provides, it has, in my opinion, this major drawback:

When writing tests for this particular class you have to inspect the class to see what are the required dependencies and must use either invoke the DI framework, even for simple tests, or use a kind of reflection mechanism to inject the dependencies (mocked / stubbed / real).

To make it even worse, the number of incoming dependencies is hidden within the class. Of course you can use tools (JDepend, etc. pp.) that tells you the number and direction of dependencies, or have a file that specifies the injection, but you must rely on this kind of tooling or inspect the class.

Another drawback I observed is, that the chance for creating a class that has multiple responsibilities is higher than compared to the situation when using Setter or Constructor Injection.
It’s like

Oh let’s use this fancy annotation to inject the service we need here … Few days / hours later: It’s so difficult to test this beast

Setter Injection

Setter injection instruments setter methods, one per dependency, that are used by the DI framework to inject the dependencies.

It’s a variant that makes the dependencies explicit and  gives you a clear overview of the dependencies of the particular class.

During testing it has the benefit that you mustn’t use the DI framework or reflection mechanisms but can directly set the dependencies.

The drawback of this approach is: You can construct a class that is in a state where it can’t work. This is because you can’t distinguish from the outside if a dependency is required or optional.

Constructor Injection

Constructor injection instruments the constructor of a class, which used by the DI framework to inject the dependencies. It is the other variant that makes dependencies explicit.

In opposite to the Setter Injection it prevents you to create a class in irregular state. (Of course you can pass null, but this is than a kind of cheating, isn’t it?)  So I would say, that this is the most strict variant:

Each dependency is mandatory

The benefits of this injection type are

  1. you have to read exactly one method, the constructor, to figure out what are the dependencies of this class.
  2. you create an immutable class which makes caching and so easier

The drawback here is, again, you can’t distinguish between optional and required dependencies. The constructor enforces that all fields are set.

The last variant I would like to discuss is

Mixing Setter and Constructor Injection

This variant, I personally prefer, mixes the Setter and Constructor Injection.

This gives you

  • the ability to distinguish between mandatory dependencies and optional on a contractual level
  • states clearly what the dependencies of a specific class are
  • enables you easily to check if a class has to much responsibilities
  • a properly configured class after construction

Testing with this kind of injection is, similar to the pure approaches, fairly simple. You don’t need the DI framework and can easily pass mocked / stubbed / real implementations into your class under test.

What to use / prefer?

This answer depends heavily on your framework / team rule / language.

But I would strongly recommend to go for one of  the explicit injection variants, because they enable you to write pure unit tests without using the dependency injection framework.

22 Kudos

2 thoughts on “Dependency Injection – Field vs Constructor vs Method

  1. I like the topic and precise comparison. Might be useful to add an example (or refer to the one in the Java EE tutorial).
    One remark: You state the improved testability, that’s correct, but then you’d change the public interface of a component for the sake of it. It’s a similar discussion as if field or method access of JPA entities should be preferred – both have advantages and disadvantages, but I think one should not introduce getters/setters just for that, but only if it is the desired behaviour from the outside.

    1. Thanks a lot.

      I completely agree with you, that only for the purpose of testing getter / setters should not be introduced.

      As I outlined, setters are okish, if the dependency is an optional one.

Leave a Reply

Your email address will not be published. Required fields are marked *