Spring Dependency Injection using Constructor with Example

Spring Dependency Injection using Constructor with Example

  • Spring Container will inject the dependencies by invoking parameterised constructor on spring beans is called Constructor based dependency Injection.
  • to do the constructor based dependency injection in applicationContext.xml we need to use <constructor-arg> tag and <value> tag. here value can be used as attribute and tag.
  • In Constructor based dependency injection to reduce the ambiguity we need add type and index attributes in applicationContext.xml

Tools Used In This Article

  • Spring 3.2.16
  • Maven 4.0.0
  • JDK 1.7

The Project Structure


Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core
  • spring-context

<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>HibernateHqlSelectDemo</groupId>
<artifactId>HibernateHqlSelectDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>>spring-core</artifactId>
<version>3.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.16.RELEASE</version>
</dependency>

</dependencies>
</project>


Account.java


package com.testpractice;

public class Account {
private int accountId;
private String name;
private String address;
public Account(int accountId, String name, String address) {
super();
this.accountId = accountId;
this.name = name;
this.address = address;
}


public void showAccountDetails(){
System.out.println("Account Id is :"+accountId);
System.out.println("Account Name is :"+name);
System.out.println("Address :"+address);
}

}


applicationContext.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-2.5.xsd">
<bean id = "account" class ="com.testpractice.Account">
<constructor-arg value = "123456" index = "0" type = "int"/>
<constructor-arg value = "abc" index = "1" type = "String"/>
<constructor-arg value = "xyz" index = "2" type = "String"/>
</bean>
</beans>

Test.java


package com.testpractice;


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


public class Test {

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
Object object =context.getBean("account");
Account account = (Account)object;

account.showAccountDetails();


}


}


output


INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.
DefaultListableBeanFactory@4ede45aa: defining beans [account]; root of factory hierarchy
Account Id is :123456
Account Name is :abc
Address :xyz

-->