Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring: Inject map of beans (key and value both are beans)

In my previous post, I explained how to inject map, where key is an integer and value is pointed to a bean. In this post, I am going to explain how to inject a map where key and value are beans.

By using , elements, key-ref and value-ref attributes, you can inject map of beans.

Example
 id="countryInfo" name="countryInfo" class="com.sample.pojo.Countries">
name="countriesMap">

key-ref="india" value-ref="indiaInfo" />
key-ref="russia" value-ref="russiaInfo" />




Following is the complete 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();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((capital == null) ? 0 : capital.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Country other = (Country) obj;
if (capital == null) {
if (other.capital != null)
return false;
} else if (!capital.equals(other.capital))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}

CountryInformation.java
package com.sample.pojo;

public class CountryInformation {
private String language;
private String currency;

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public String getCurrency() {
return currency;
}

public void setCurrency(String currency) {
this.currency = currency;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CountryInformation [language=").append(language).append(", currency=").append(currency)
.append("]");
return builder.toString();
}

}

Countries.java
package com.sample.pojo;

import java.util.Map;

public class Countries {
private MapCountry, CountryInformation> countriesMap;

public MapCountry, CountryInformation> getCountriesMap() {
return countriesMap;
}

public void setCountriesMap(MapCountry, CountryInformation> countriesMap) {
this.countriesMap = countriesMap;
}

}

myConfiguration.xml

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">

id="india" class="com.sample.pojo.Country">
name="name" value="India" />
name="capital" value="New Delhi" />


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


id="russiaInfo" class="com.sample.pojo.CountryInformation">
name="language" value="Russian" />
name="currency" value="Ruble" />



id="indiaInfo" class="com.sample.pojo.CountryInformation">
name="language" value="Hindi" />
name="currency" value="Rupee" />


id="countryInfo" name="countryInfo" class="com.sample.pojo.Countries">
name="countriesMap">

key-ref="india" value-ref="indiaInfo" />
key-ref="russia" value-ref="russiaInfo" />






HelloWorld.java
package com.sample.test;

import java.util.Set;

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("countryInfo", Countries.class);

SetCountry> countrySet = countries.getCountriesMap().keySet();

for (Country country : countrySet) {
System.out.println(country + " : " + countries.getCountriesMap().get(country));
}

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

Run ‘HelloWorld.java’, you can able to see following output.
Country [name=India, capital=New Delhi] : CountryInformation [language=Hindi, currency=Rupee]
Country [name=Russia, capital=Moscow] : CountryInformation [language=Russian, currency=Ruble]




Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring: Inject map of beans (key and value both are beans)

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×