Saturday, July 9, 2011

Injecting enums to a map - spring

      Assume an enum class MyEnum is to be used as keys of a java.util.Map and injected in the spring configuration file. This can be done as follows;


public enum MyEnum {
          A, B, C, D

The above enum can be injected to a Map in spring;

<util:map id="myMap" key-type="">
      <entry key="A" value="value a" />
      <entry key="B" value="value b" />
      <entry key="C" value="value c" />
      <entry key="D" value="value d" />


This map can be injected to a class.


public class MyClass {

     private Map<MyEnum, String> myMap;

     public void doIt() {
          //do something  ....  
    public void setMyMap(Map<MyEnum, String> myMap) {
         this.myMap = myMap;    

Spring configuration of MyClass.

<bean id="myClass" class="">
     <property name="myMap" ref="myMap">


No comments:

Post a Comment