setter and constructor injection in spring

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

Alternative to setter injection
Setter injection is a straightforward way to configure, wire bean properties.
But one shortcoming of setter injection is that assumes that all mutable properties
are available via a setter method. You may not want all of your beans to
behave this way. For one thing, when this type of bean is instantiated, none of its
properties have been set and it could possibly be in an invalid state.
 Second, you
may want certain properties to be set just once when the bean is created and
become immutable after that point. This is complicated, if not impossible, when
exposing all properties through setters.
An alternative is to design your beans where some properties are set via constructors.
This is a particularly good design if some properties are required and
immutable, such as a DAO s DataSource.

So if you choose to design some of your
beans this way or are working with beans that are already designed this way, you
still need a way to configure these objects through Spring. use constructor injection to set the minimal properties for
a bean.

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

    Expert Comment

    you don't always want to add setters to your bean, for example you may not want a property to be changed.
    For spring to inject values into these beans you can use constructor injection instead of using the setter
    LVL 7

    Author Comment

    >>you don't always want to add setters to your bean, for example you may not want a property to be changed.

    can you please elaborate on this.
    LVL 92

    Accepted Solution

    some properties should not be changed, for example an id.
    in these cases you don't want to have a setter for that property (as it would allow the property to be changed)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    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

    11 Experts available now in Live!

    Get 1:1 Help Now