setter vs constructor injection spring

Posted on 2011-05-04
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.
Question by:gudii9
    LVL 23

    Accepted Solution

    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.
    LVL 59

    Assisted Solution

    by:Kevin Cross

    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 ( 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:

    There is also this Article for reference:

    Hope that helps!
    LVL 59

    Expert Comment

    by:Kevin Cross
    Agree, @cmalakar.
    LVL 7

    Author Comment

    i was not clear on optional and mandatory parameters. can you  please elaborate on them
    LVL 7

    Author Comment

    i mean optional and mandatory properties
    LVL 23

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now