spring injection variable setter constructor

1 field.

    private Point center;
2 setter.
    public void setCenter(Point center) {
        this.center = center;

If you weren’t using autowiring, there is a big difference between constructor and setter injection. You write the XML differently in order to inject the dependencies. And setter injection dependencies are optional while constructor injection dependencies are not.

With autowiring, the only reason I can think of is to avoid a circular dependency problem. If A has B has an autowired dependency to the constructor and B has the same for A, we can’t instantiate either of them. Giving one a setter dependency could help with that. (Just speculating, I haven’t tried this scenario). I’m curious to see if someone has a better reason.

The first way is the more modern approach and that used today in new projects. The second way is the older traditional way, the result is the same. Both are done via reflection. As a matter of fact if your java security policy allows (usually does) the setter is not even required to be there for the @Autowire to work. Sometimes people will use it on constructors to enforce non-optional dependencies, although @Autowired fields are required unless explicitly marked as optional, so once again the result is largely the same. You can also annotate non-constructor/setter methods with @Autowired.

Spring is pretty good about handling cyclic dependencies to a degree but will throw an error if it can’t resolve it. These error spell out the problem in a pretty detailed manner.

Another article about constructor/setter

3 constructor.
    public Circle(Point center) {
        this.center = center;

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.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s