?
Solved

setter vs constructor injection spring

Posted on 2011-05-04
6
Medium Priority
?
394 Views
Last Modified: 2012-05-11
I was reading folowing lines and I did not understand clearly.
 Goood yardstick to go
by is the clarity of your Spring configuration file. For instance, if you are creating
a bean that has only one mandatory property (such as a DAO object as its Data-
Source), constructor injection would probably be a good choice. on the other
hand, if you have a bean that has multiple, optional properties (such as the Data-
Source itself), setter injection would be more appropriate.
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
cmalakar earned 1000 total points
ID: 35699040
Using Constructors, for Objects with mandatory properties will make sure that instance will be in proper state always.

On the other hand, Using constructors for optional properties will be of no use. Setter methods are much appropriate for optional properties.
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 total points
ID: 35699055
gudii9,

Do you understand the main concept?  If you don't, the basic principle here is you want to strive for loose coupling of classes and depencies (or the differing implementation thereof).  Think abstraction and interfaces.

The above is giving you an idea on how to determine which type of Dependency Injection (http://en.wikipedia.org/wiki/Dependency_injection) may be best.  In other words, do you pass everything needed in the constructor of your class or later through a setter after the class has been constructed already.  If a property is optional, then it makes sense to make the construction of simpler, i.e., no required paramater and then later if optional value is desired then it will be set.

Per the Wikipedia article linked, you will find that there are three types: interface, setter and constructor.  The dependency framework uses this type to inject dependencies at runtime.

Martin Fowler has a nice Article on the topic:
http://martinfowler.com/articles/injection.html

There is also this Article for reference:
http://java.dzone.com/articles/constructor-injection-vs-0

Hope that helps!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 35699065
Agree, @cmalakar.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 35699988
i was not clear on optional and mandatory parameters. can you  please elaborate on them
0
 
LVL 7

Author Comment

by:gudii9
ID: 35699997
i mean optional and mandatory properties
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35700022
In this context..

Mandatory properties that are must required to create the object
Optional properties are those, that may or may not required to create the object.

For example,  In an employee object, employee name can be a mandatory property
And employee address can be optional property.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question