A Developer's Diary

Dec 28, 2012

Spring Framework - Autowiring a map, list or array using Annotation

You can auto-wire a particular property in spring, using @Autowired annotation. For using @Autowired annotation, you will have to register AutowiredAnnotationBeanPostProcessor bean instance in the spring IOC container

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
OR
You can simply include the <context:annotation-config /> element in your bean configuration file. The <context:annotation-config /> automatically registers an instance of AutowiredAnnotationBeanPostProcessor for you

For including <context:annotation-config /> element, you need to include context namespace in the bean definition file
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

Auto wiring is supported for
1. Constructors
2. Setter Methods
3. Arbitrary Methods
4. Fields

Spring container can autowire a Map, List or an Array with all the beans defined in the bean configuration file whose types are compatible.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

  <bean id="company" class="com.examples.spring.Company">
    <property name="name" value="mycompany" />
  </bean>

  <bean id="key1" class="com.examples.spring.Employee">
    <constructor-arg name="name" value="Allan Donald" />
  </bean>
  <bean id="key2" class="com.examples.spring.Employee">
    <constructor-arg name="name" value="Sachin Tendulkar" />
  </bean>
  <bean id="key3" class="com.examples.spring.Employee">
    <constructor-arg name="name" value="Steven Waugh" />
  </bean>
</beans>

The Company class demonstrating the use of @Autowired annotation to populate a Map, a List and an Array
package com.examples.spring;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

public class Company
{
    private String companyName;

    private Map<String, Employee> mapOfEmployees;
    private List<Employee> listOfEmployees;
    private Employee[] arrayOfEmployees;

    public void setName(String name)
    {
        this.companyName = name;
    }

    public String getName()
    {
        return companyName;
    }

    @Autowired
    public void setEmployees(Map<String, Employee> employees)
    {
        this.mapOfEmployees = employees;
    }

    @Autowired
    public void setEmployees(Employee[] employees)
    {
        this.arrayOfEmployees = employees;
    }

    @Autowired
    public void setEmployees(List<Employee> employees)
    {
        this.listOfEmployees = employees;
    }

    public Map<String, Employee> getMapOfEmployees()
    {
        return mapOfEmployees;
    }

    public List<Employee> getListOfEmployees()
    {
        return listOfEmployees;
    }

    public Employee[] getArrayOfEmployees()
    {
        return arrayOfEmployees;
    }
}

The Employee class definition
package com.examples.spring;

public class Employee {
 private String empName;

 public Employee(String name) {
  this.empName = name;
 }

 public String getName() {
  return empName;
 }
}

The client program prints the list of employees in the Map, List and Array populated earlier using the springs auto-wiring method.
package com.examples.spring;

import java.util.List;
import java.util.Map;

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

public class App
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Company company = (Company)context.getBean("company");

        for (int i = 0; i < 2; ++i)
            System.out.println();

        System.out.println("Displaying Employee Map");
        Map<String, Employee> mapOfEmployees = company.getMapOfEmployees();
        for (Map.Entry<String, Employee> entry : mapOfEmployees.entrySet())
        {
            System.out.println("Key: " + entry.getKey() + " Name: " + entry.getValue().getName());
        }

        System.out.println();
        System.out.println("Displaying Employee List");
        List<Employee> listOfEmployees = company.getListOfEmployees();
        for (Employee emp : listOfEmployees)
        {
            System.out.println("Name: " + emp.getName());
        }

        System.out.println();
        System.out.println("Displaying Employee Array");
        Employee[] arrayOfEmployees = company.getArrayOfEmployees();
        for (Employee emp : arrayOfEmployees)
        {
            System.out.println("Name: " + emp.getName());
        }
    }
}

Sample Run

2 comments :

Anonymous said...

Thanks, nice post.

Unknown said...

Indeed a best post

Post a Comment