Constructor Injection dependency Spring

Constructor Injection dependency Spring

Constructor Injection Dependency Example Screenshots given in Simple terms

In the first program of Spring, Developing a Simple Spring Application – Learn Step-by-Step, both IoC dependencies through Constructor and Setter methods are given. But in this example, more emphasis is given on injection Constructor injection with precautions where a Developer can go into problems if not used properly.

Note: Before going into the details of this program, it is advised to go though the first program.

1. Bean Program:

Instead of displaying output with printXXX() methods of First Program – A., we have overridden the toString() method of Object class whose job is to just print the variables values.

Program-wise it is simpler than the first one. Let us write the XML file for the above bean with constructor injection dependency .

Example on Constructor Injection

2. XML file: applicationContext.xml (this file gives values to variables of

3. Client Program:

Console screenshot when the above client program is run.

Constructor Injection

Now our interest is in Constructor injection code.

In the bean, we have only one constructor. In the above code, the order of values given must be in the same order of constructor parameters (by data type-wise).

If we have multiple constructors, then how to do the job? No problem as long as constructors are declared with different number of parameters. In Spring, sometimes, if constructors with the same number of parameters, of course with different data types exist, problem arises.

Observe the following two constructors.

The bean configuration will be like this.

<bean id="std1" class="Student" >
<constructor-arg value="45"/>
<constructor-arg value="Aurora"/>

With the above configuration, which constructor you may expect to call? Definitely you say it is the first constructor (with int, String) with the previous Spring experience. To your surprise, the second constructor with String, String arguments is called. Why? It is because Spring assumes both the values as strings and calls the second constructor as it is available. OK, then how to call the first one in this scenario. Just use type attribute in element.

<bean id="std1" class="Student" >
<constructor-arg type="int" value="45"/>
<constructor-arg type="java.lang.String" value="Aurora"/>

Now the first constructor is called.

Let us take a different scenario. Assume we have the following two constructors.

The configuration file is like this.

If I ask you which constructor is called, definitely you say, the second one (int, String). Sorry, the first constructor is called. Why? Spring has a habit of not considering the order of arguments that appear in the configuration file while calling the constructor. Now comes index attribute to specify the argument index of element. Observe the usage of index attribute.

Now, the second constructor is called.

With this knowledge, let us rewrite our first XML file as follows.

As the index numbers are given, the order of arguments can be of any order. This is the best way to write when multiple constructors exist (with same number of parameters).

The programmer must be very cautious. If a wrong order is given, if compatible constructors exist, it may result in wrong output (and not error) like, for name it may come college value and for college, name value may come. Try all the combinations on constructor Injection dependency.

One thought on “Constructor Injection dependency Spring

  1. Rajeswari K


    What a wonderful site..Just wow….
    I have a question here.

    W.r.t the last code snippet for constructor index section,the construction called will be the one with (String ,int) param fashion .


Leave a Reply

Your email address will not be published. Required fields are marked *