Home » Spring » Constructor Injection with primitive and String-based values

Constructor Injection with primitive and String-based values

Let us see an example of injecting primitive and String based values by using constructor injection.Consider the following Employee bean class.

Creating Bean Class

Create a class Employee.java inside the package "com.jwt.spring.bean".

Employee.java

package com.jwt.spring.bean;

public class Employee {
	private String name;
	private int age;
	private String country;
	public Employee(String name, int age, String country) {
		this.name = name;
		this.age = age;
		this.country = country;
	}
	public String toString() {
		return "Employee Name is" + name + " and employee Age is " + age
				+ " years, lives in " + country;
	}
}

In the above class there are three attributes viz. name, age and country. All the three attributes are set through constructor injection. The toString() method of the class is used to display the Employee information.

Configure bean in configuration xml file

Below is the code snippet to use the Set in the spring bean configuration file.

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="employee" class="com.jwt.spring.bean.Employee">
		<constructor-arg value="Mukesh" />
		<constructor-arg value="30" />
		<constructor-arg value="India" />
	</bean>
</beans>

Creating Test Class

Create a class Test.java to test the program.

Test.java

package com.jwt.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"bean.xml");
		Employee employee = (Employee) context.getBean("employee");
		System.out.println(employee);
	}
}

Output:
Employee Name is Mukesh and employee Age is 30 years, lives in India

Constructor injection type ambiguities in Spring

In the above example there is only one constructor in the Employee class, this code will work fine. But when your class contains multiple constructors with same number of arguments, it will cause the constructor injection argument type ambiguities problem.

Now add two constructor with the same number of arguments in our Employee bean class. Updated Employee bean class is given below.

package com.jwt.spring.bean;

public class Employee {
	private String name;
	private int age;
	private String city;
	public Employee(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public Employee(int age, String city) {
		this.age = age;
		this.city = city;
	}
	public String toString() {
		return "Employee Name is " + name + " and employee Age is " + age
				+ " and employee city is " + city;
	}
}

Bean configuration is given below.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="employee" class="com.jwt.spring.bean.Employee">
		<constructor-arg value="24" />
		<constructor-arg value="Mukesh" />
	</bean>
</beans>

Now which constructor do you think will be invoked? The second one with the int and the String argument, right? But for your surprise it will call the first constructor with String and int argument . To avoid this confusion you need to specify the type attribute of the constructor-arg element. Now with the following bean configuration, the first constructor will be invoked.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="employee" class="com.jwt.spring.bean.Employee">
		<constructor-arg type="int" value="24"/>
        <constructor-arg type="java.lang.String" value="Mukesh"/>
	</bean>
</beans>

Now which constructor do you think will be called? The second constructor, right? But again for your surprise the first constructor will be called, this is because the order in which the arguments appear in the bean configuration file will not be considered while invoking the constructor. To solve this problem you can use the index attribute to specify the constructor argument index.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="employee" class="com.jwt.spring.bean.Employee">
		<constructor-arg index="0" value="24" />
		<constructor-arg index="1" value="Chennai" />
	</bean>
</beans>


Previous Next Article
comments powered by Disqus