Friday 5 May 2017

Spring: @Autowired: Inject beans into map

By using @Autowired annotation, you can inject beans into map, as long as the key type of the map is string. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names.

Example
public class Authors {

 @Autowired
 private Map<String, Author> authors;

 public Map<String, Author> getAuthors() {
  return authors;
 }

}


Following is the complete working application.

Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

Authors.java
package com.sample.pojo;

import java.util.Map;
import java.util.Set;

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

public class Authors {

 @Autowired
 private Map<String, Author> authors;

 public Map<String, Author> getAuthors() {
  return authors;
 }

 public void printAuthors() {
  Set<String> keys = authors.keySet();

  for (String key : keys) {
   System.out.println(key + " : " + authors.get(key));
  }
 }
}

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" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

 <context:annotation-config />

 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <bean id="krishna" class="com.sample.pojo.Author">
  <property name="firstName" value="hari krishna" />
  <property name="lastName" value="Gurram" />
  <property name="dateOfBirth" value="05 may 1989" />
  <property name="country" value="India" />
 </bean>

 <bean id="Kiran" class="com.sample.pojo.Author">
  <property name="firstName" value="Kira" />
  <property name="lastName" value="Kumnoor" />
  <property name="dateOfBirth" value="25 August 1990" />
  <property name="country" value="India" />
 </bean>

 <bean id="authors" class="com.sample.pojo.Authors" />

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Authors;

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

  Authors authors = context.getBean("authors", Authors.class);

  authors.printAuthors();

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

Output
osho : Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
krishna : Author [firstName=hari krishna, lastName=Gurram, dateOfBirth=05 may 1989, country=India]
Kiran : Author [firstName=Kira, lastName=Kumnoor, dateOfBirth=25 August 1990, country=India]


Previous                                                 Next                                                 Home

No comments:

Post a Comment