Spring: Constructor Injection by Example
In our last tutorial we have seen about Setter Injection by simple example. In this tutorial we see same example with Constructor Injection. Only thing which we need to take care is "constructor-arg" order and type should match with bean class constructor method. Otherwise constructor injection argument type ambiguities exception will be thrown. |
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.samples</groupId> <artifactId>SimpleSetterInj</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.10.RELEASE</version> </dependency> </dependencies> </project>
Employee.java
package com.app.springcore; public class Employee { private String empName; private int age; private String gender; public Employee(String empName, int age, String gender){ this.empName = empName; this.age = age; this.gender = gender; } public String getEmpName() { return empName; } public int getAge() { return age; } public String getGender() { return gender; } }
Office.java
package com.app.springcore; public class Office { private String offName; private String offAddress; private Employee employee; public Office(String offName, String offAddress, Employee employee){ this.offName = offName; this.offAddress = offAddress; this.employee = employee; } public String getOffName() { return offName; } public String getOffAddress() { return offAddress; } public Employee getEmployee() { return employee; } }
Spring-Core.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="emp" class="com.app.springcore.Employee"> <constructor-arg><value>Steve</value></constructor-arg> <constructor-arg><value>35</value></constructor-arg> <constructor-arg><value>Male</value></constructor-arg> </bean> <bean id="office" class="com.app.springcore.Office"> <constructor-arg><value>ABCD Crop.</value></constructor-arg> <constructor-arg><value>Bangalore, India</value></constructor-arg> <constructor-arg ref="emp"></constructor-arg> </bean> </beans>
TestClass.java
package com.app.springcore; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestClass { public static void main(String[] args) { ApplicationContext appCon = new ClassPathXmlApplicationContext("Spring-Core.xml"); Office office = (Office)appCon.getBean("office"); System.out.println("Office Name : "+office.getOffName()); System.out.println("Office Address : "+office.getOffAddress()); System.out.println("Employee Name : "+office.getEmployee().getEmpName()); System.out.println("Employee Age : "+office.getEmployee().getAge()); System.out.println("Employee Gender: "+office.getEmployee().getGender()); ((ConfigurableApplicationContext)appCon).close(); } }
OUTPUT:
Office Name : ABCD Crop. Office Address : Bangalore, India Employee Name : Steve Employee Age : 35 Employee Gender: Male