Spring Dependency Injection using Setter Method with Example

Spring Dependency Injection using Setter Method with Example

  • Spring Container will inject the dependencies by invoking setter methods on spring beans is called Setter based dependency Injection.
  • to do the setter based dependency injection in applicationContext.xml we need to use <property> tag and <value> tag. here value can be used as attribute and tag.

Tools Used In This Article

  • Spring-core 3.6.10
  • Maven 4.0.0
  • JDK 1.7

The Project Structure


Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core

<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 int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
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">

<property name="accountId" value = "123456"/>
<property name = "name" value = "abcdfdfeg"/>
<property name = "address" value = "xyz"/>

</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@20d675ec: defining beans [account]; root of factory hierarchy
Account Id is :123456
Account Name is :abcdfdfeg
Address :xyz
-->