setter and constructor injection in spring
Posted on 2011-05-04
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.
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
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.