Spring - Injecting Collections into bean example

Posted on January 2, 2017


Spring framework provides <list/>, <set/>, <map/> and <props/> elements to inject collections type List, Set, Map and Properties respectively. These collections types are injected via either <property/> or <constructor-arg/> element.

In previous post, we learned about <props/> element. In this post, we will learn how to inject the list, set and map into spring bean.

<list/> element example

Consider the following CustomerService bean with attribute names.

CustomerService.java

package com.boraji.tutorial.spring;

import java.util.List;

public class CustomerService {
   private List<String> names;

   public List<String> getNames() {
      return names;
   }

   public void setNames(List<String> names) {
      this.names = names;
   }
}

Declare CustomerService bean in XML configuration metadata and add values in the list using child element <value/> of <list/> element as shown below.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

  <bean id="customerService" class="com.boraji.tutorial.spring.CustomerService">
    <property name="names">
      <list>
        <value>Sunil</value>
        <value>Robinb</value>
        <value>Elezabeth</value>
      </list>
    </property>
  </bean>

</beans>

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import java.util.List;

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

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      @SuppressWarnings("resource")
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      CustomerService service = context.getBean("customerService", CustomerService.class);
      List<String> name=service.getNames();
      System.out.println(name);
   }
}

Output

[Sunil, Robinb, Elezabeth]

 


<set/> element example

Consider the following CustomerService bean with attribute customerIds.

CustomerService.java

package com.boraji.tutorial.spring;

import java.util.Set;

public class CustomerService {
   private Set<Long> customerIds;

   public Set<Long> getCustomerIds() {
      return customerIds;
   }

   public void setCustomerIds(Set<Long> customerIds) {
      this.customerIds = customerIds;
   }
}

Declare the CustomerService bean in XML configuration metadata and add values in the set using child element <value/> of <set/> element as shown below. 

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

  <bean id="customerService" class="com.boraji.tutorial.spring.CustomerService">
    <property name="customerIds">
      <set>
        <value>1</value>
        <value>2</value>
        <value>3</value>
      </set>
    </property>
  </bean>

</beans>

Create manin class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import java.util.Set;

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

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      @SuppressWarnings("resource")
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      CustomerService service = context.getBean("customerService", CustomerService.class);
      Set<Long> ids=service.getCustomerIds();
      System.out.println(ids);
   }
}

Output

[1, 2, 3]

 


<map/> element example

Consider the following CustomerService bean with attribute address.

CustomerService.java

package com.boraji.tutorial.spring;

import java.util.Map;

public class CustomerService {

   private Map<String, String> address;

   public Map<String, String> getAddress() {
      return address;
   }

   public void setAddress(Map<String, String> address) {
      this.address = address;
   }

}

Declare CustomerService bean in XML configuration metadata and add key-value entry in the map using key and value attribute of <entry/> element as shown below.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

  <bean id="customerService" class="com.boraji.tutorial.spring.CustomerService">
    <property name="address">
      <map>
        <entry key="CITY" value="Bengaluru"/>
        <entry key="STATE" value="Karnataka"/>
        <entry key="COUNTRY" value="India"/>
      </map>
    </property>
  </bean>

</beans>

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import java.util.Map;

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

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      @SuppressWarnings("resource")
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      CustomerService service = context.getBean("customerService", CustomerService.class);
      Map<String, String> map=service.getAddress();
      System.out.println(map);
   }
}

Output

{CITY=Bengaluru, STATE=Karnataka, COUNTRY=India}

Download this example from the below link..

Download Sources