constructor injection spring

Posted on 2011-05-04
Last Modified: 2012-05-11
I was reading folowing lines and I did not understand clearly.

Injecting dependencies via constructor

A class can have one or more constructors, each taking a unique set of
arguments. With that in mind, you can program your bean classes with constructors
that take enough arguments to fully define the bean at instantiation.
Using constructors this way, it is impossible to create a bean without it being
ready to use.
Whereas the shortcoming of setter injection is that it is not clear which properties
are required and which are optional, constructor injections main strength
is in the strong dependency contract imposed by constructors. That is, constructors
make it virtually impossible to instantiate a bean that is not fully defined and
ready to use.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
Question by:gudii9
    LVL 92

    Expert Comment

    constructor injection allows you to control exactly what constructor is called and what values are required.
    With setter injection you cannot specify what propertys must be set and which are optional
    LVL 47

    Expert Comment

    The main point they are making here that using contsructor injcetion it is impossible to create bean which
    may be invalid - lacking some properties without which it cannot function, wheras with setter injection
    that may be possible.

    LVL 7

    Author Comment

    >>>With setter injection you cannot specify what propertys must be set and which are optional

    why we cannot specify in setter. please elaborate on this.
    LVL 47

    Accepted Solution

    If you leave all properies to be specified in setters you can end up in
    the situation where some properites were overlooked and not specified,
    whereas these properties are vital for the life of this bean.
    If you specify those vital properties in constructor
    you may not even create a bean without those properties set and therefore you'll
    never have to encounter the "crippled" bean which would lack some vital
    properties. This emphasizes the advantage of contructor injection.
    LVL 92

    Assisted Solution

    > why we cannot specify in setter. please elaborate on this.

    because each property has its own setter

    a constructor can take any number of paranmeters allowing it to control what is required to create the object

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    NotAlone Challenge 20 65
    scoresSpecial  challenge 13 35
    sumHeights2  challenge 7 51
    Modeling a class in java 5 23
    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…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now