[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


constructor injection spring

Posted on 2011-05-04
Medium Priority
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
  • 2
  • 2
LVL 92

Expert Comment

ID: 35695022
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

ID: 35695024
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.


Author Comment

ID: 35700109
>>>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

for_yan earned 1000 total points
ID: 35700190
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

objects earned 1000 total points
ID: 35702333
> 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

Technology Partners: 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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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