Sunday, 5 March 2017

Spring: Inject list of beans

In my previous post, I explained how to inject list of strings. By using <list> and <ref> elements, you can inject list of beans. Following example explains this.

Example
 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="favoriteCountries">
   <list>
    <ref bean="india" />
    <ref bean="russia" />
   </list>
  </property>
 </bean>

Following is the working application.

Country.java
package com.sample.pojo;

public class Country {
 private String name;
 private String capital;

 public String getName() {
  return name;
 }

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

 public String getCapital() {
  return capital;
 }

 public void setCapital(String capital) {
  this.capital = capital;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Country [name=").append(name).append(", capital=").append(capital).append("]");
  return builder.toString();
 }

}

Countries.java
package com.sample.pojo;

import java.util.List;

public class Countries {
 private List<Country> favoriteCountries;

 public List<Country> getFavoriteCountries() {
  return favoriteCountries;
 }

 public void setFavoriteCountries(List<Country> favoriteCountries) {
  this.favoriteCountries = favoriteCountries;
 }

}

myConfiguration.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="india" class="com.sample.pojo.Country">
  <property name="name" value="India" />
  <property name="capital" value="New Delhi" />
 </bean>

 <bean id="russia" class="com.sample.pojo.Country">
  <property name="name" value="Russia" />
  <property name="capital" value="Moscow" />
 </bean>


 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="favoriteCountries">
   <list>
    <ref bean="india" />
    <ref bean="russia" />
   </list>
  </property>
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import java.util.*;

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

import com.sample.pojo.Countries;
import com.sample.pojo.Country;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Countries countries = context.getBean("demoCountryCapitals", Countries.class);

  List<Country> favoriteCountries = countries.getFavoriteCountries();

  for (Country country : favoriteCountries) {
   System.out.println(country);
  }

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.
Country [name=India, capital=New Delhi]
Country [name=Russia, capital=Moscow]



Previous                                                 Next                                                 Home

No comments:

Post a Comment