Showing posts with label Setter Injection. Show all posts
Showing posts with label Setter Injection. Show all posts

Spring: Setter Injection by Example

Core of the Spring Framework is its Inversion of Control (Ioc) container. The IoC container manages java objects – from instantiation to destruction – through its BeanFactory. Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, life-cycle events, and any AOP features.
Dependency Injection with Spring
The IoC container enforces the dependency injection pattern for components by leaving them loosely coupled and allowed to code to abstractions. It exits in two major types like

  • Setter Injection
  • Constructor Injection

In this tutorial lets discuss about Setter Injection with simple example


<project xmlns="" xmlns:xsi=""

  <!-- Spring Context -->


public class Employee {

 private String empName;
 private int age;
 private String gender;
 public String getEmpName() {
  return empName;
 public void setEmpName(String empName) {
  this.empName = empName;
 public int getAge() {
  return age;
 public void setAge(int age) {
  this.age = age;
 public String getGender() {
  return gender;
 public void setGender(String gender) {
  this.gender = gender;


public class Office {

 private String offName;
 private String offAddress;
 private Employee employee;
 public String getOffName() {
  return offName;
 public void setOffName(String offName) {
  this.offName = offName;
 public String getOffAddress() {
  return offAddress;
 public void setOffAddress(String offAddress) {
  this.offAddress = offAddress;
 public Employee getEmployee() {
  return employee;
 public void setEmployee(Employee employee) {
  this.employee = employee;


<beans xmlns=""
    <bean id="emp" class="">
        <property name="empName" value="Steve"></property>
        <property name="age"><value>35</value> </property>
        <property name="gender" value="Male"></property>
    <bean id="office" class="">
        <property name="offName" value="ABCD Crop."></property>
        <property name="offAddress" value="Bangalore, India"></property>
        <property name="employee" ref="emp"></property>


import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

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());


Office Name    : ABCD Crop.
Office Address : Bangalore, India
Employee Name  : Steve
Employee Age   : 35
Employee Gender: Male

You can download sample project in this link.