Spring Setter Injection Example

Spring Setter Injection Example


                     In this example we have created a java project named SpringProject. This project has three java classes namely Address(Pojo), Employee(Pojo) and EmployeeAddress(main class) in package com.thecafetechno and spring configuration file spring-config.xml in src as shown in the project explorer of eclipse.
In this project we used the following jar files:
  • spring-core-3.0.2.RELEASE.jar
  • spring-beans-3.0.6.RELEASE.jar
  • commons-logging-1.1.jar
Download these jars from here and configure build path of your project to include these jars.
To configure build path Right-Click SpringProject > Build Path > Configure Build Path
As shown in fig below..
A new dialogue box will open ; here select “Add External Jars” browse to select the jars and click open and the click OK.

Employee.java :

The code of Employee.java is given below. In this we have declared dependency on Address to explain DI. We have overridden the toString() method to print object of Employee class meaningfully.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.poosan;
public class Employee {
        private String name;
        private int age;
        private Address address;
        @Override
        public String toString() {
            return "Name :"+name+"\n"+"Age :"+age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Address getAddress() {
            return address;
        }
        public void setAddress(Address address) {
            this.address = address;
        }
}

Address .java :

The code of Address.java is given below. We have override the toString() method to print object of Address meaningfully.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.poosan;
public class Address {
    private String city;
    private String state;
    @Override
    public String toString() {
        return "City: "+city+"\n"+"State: "+state;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

EmployeeAddress .java :

EmployeeAddress class contains the main method. Here we have instantiated the Spring container and got the employeeBean.
To instantiate the container we need to give the path of spring-config.xml to BeanFactory. We referred to spring-config.xml using ClassPathResource ClassPathResource resource = new ClassPathResource(“spring-config.xml”);
And this resource is passed as argument to XmlBeanFactory constructor.
BeanFactory factory = new XmlBeanFactory(resource);
Now Factory is loaded with beans defined in sprin-config.xml file we can get any bean using getBean() method of BeanFactory by passing beanId to getBean(“beanId”).
We only got the employeeBean but spring injected the addressBean into Employee and we printed the both employee object and injected address object.
We are not creating the address object using ‘Address address=new Address()’,it’s the spring who is creating and setting it into employee object which is called Dependency Injection. See code below..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.poosan;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class EmployeeAddress {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource
                                         ("spring-config.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        Employee employee=(Employee)factory.getBean("employeeBean");
        System.out.println("Employee: "+employee);
        System.out.println("Address: "+employee.getAddress());
    }
}

OutPut :

INFO: Loading XML bean definitions from file [F:\java_program\SpringProject\src\spring-config.xml]
Employee: Name : poovarasan
Age :21
Address: City: vellore
State: tamil nadu
To run this example download code , import it into eclipse and configure build path by adding jars to classpath.

SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment