aspose file tools*
The moose likes Spring and the fly likes Difference Between Constructor and Setter Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Difference Between Constructor and Setter Injection" Watch "Difference Between Constructor and Setter Injection" New topic

Difference Between Constructor and Setter Injection

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48

I was reading this post on why people use setter injection more than constructor injection.But I could not understand the first reason as well.

Spring maintained its focus on setter injection because at the time, we believed that the lack of default arguments and argument names for constructor arguments resulted in less clarity for developers

can somebody please explain me the first reason stated above?

Any help would be appreciated.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1632

The reference you quoted is simply stating that constructor args do not have names in the spring configuration XML, which can make it less clear for developers. That said the thread you were looking at is quite old and the new and more preferred approach today is Spring's annotation based field injection using @Autowired, @Inject, and @Resource. In this case setters are not required.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: Difference Between Constructor and Setter Injection
Similar Threads
Is not autowire a another type of dependendcy injection?
Not sure about exact difference between Constructor Injection and Setter Injection
Dependency checking and constructor injection
Dependency Injection with setter injection ?
DI - Constructor injection / Setter Injection ?