Over a million developers have joined DZone.

Repeat After Me: Setter Injection is a Symptom of Design Problems

DZone's Guide to

Repeat After Me: Setter Injection is a Symptom of Design Problems

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

I just stumbled across an article by Steve Schols in which he compares dependency injection via setter with constructor injection. And he prefers setter injection.

I don’t agree. I especially do not agree with his arguments. Lets take them one by one.

What are the problems with constructor injection?
No reconfiguration and re-injection

Why would you want to do that? One reason why many programs are so difficult to think about is that stuff changes around. Think about it. If the state of an object is defined by its constructor you just have to find the place where your object got created in order to understand how it looks like at run time. If the state can be changed through setters you have to find every place where the setter gets called AND if the program control actually came across these places AND in which order in order to understand what state your object is in.

So don’t change the state of your objects.

you could create a circular dependency using Spring. But not by using constructor-based injection

Excuse me? How is this possibly a drawback. I spent a lot of time to fix problems caused by circular dependencies. Using constructor injection prevents many circular dependencies. Absolutely a good thing.

He also quotes the spring reference documentation:

The Spring team generally advocates setter injection, because large numbers of constructor arguments can get unwieldy

Again constructor injection shows you you have a problem. I have yet to see a class that has more then three dependencies which adheres to the Single Responsibility Principle.

So if you run into problems with constructor injection congratulation! You just found a strong indication of a design problem in your application. Go ahead fix the design problem and proceed using constructor injection.

Of course there are situations where you can’t fix the design problem and in these cases setter injection is a valid workaround. But it is just that: A workaround.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}