Announcement Announcement Module
Collapse
No announcement yet.
Using enum in the application context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using enum in the application context

    Hi everyone, I want to use enums as keys in a map within the context.xml. The following doesn't work. Any ideas on what I should do ? Thanks. Martin.

    Code:
    <bean id="mybean" class="com.abc.MyBean">
       <property name="mappers">
          <map>
             <entry>
                <key>
                   <bean class="com.abc.Colour.RED"/>
                </key>
                <value>
                   <bean class="com.abc.RedMapper"/>
                </value>
             </entry>
          </map>
       </property>
    </bean>

  • #2
    Hi Martin,

    Originally posted by sylvestris View Post
    I want to use enums as keys in a map within the context.xml. The following doesn't work. Any ideas on what I should do?
    Yes. First and foremost, "com.abc.Colour.RED" is not a class. So that configuration could never work.

    You need to access the RED enum constant as a static field. This can be achieved with the util namespace as follows:

    Code:
    <?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:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    	<util:constant id="red" static-field="com.abc.Colour.RED" />
    
    	<bean id="redMapper" class="com.abc.RedMapper" />
    
    	<bean id="mybean" class="com.abc.MyBean">
    		<property name="mappers">
    			<map>
    				<entry key-ref="red" value-ref="redMapper" />
    			</map>
    		</property>
    	</bean>
    
    </beans>
    To prove that it works, I threw together a mini unit test using the following classes:

    Code:
    package com.abc;
    
    public enum Colour {
    	RED, GREEN, BLUE;
    }
    Code:
    package com.abc;
    
    public interface ColourMapper {}
    Code:
    package com.abc;
    
    public class RedMapper implements ColourMapper {}
    Code:
    package com.abc;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class MyBean {
    
    	private final Map<Colour, ColourMapper> mappers = new HashMap<Colour, ColourMapper>();
    
    	public Map<Colour, ColourMapper> getMappers() {
    		return this.mappers;
    	}
    
    	public void setMappers(Map<Colour, ColourMapper> mappers) {
    		this.mappers.putAll(mappers);
    	}
    }
    Code:
    package com.abc;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class EnumColourMapperTests {
    
    	@Autowired
    	private MyBean myBean;
    
    	@Test
    	public void colourMappingConfig() {
    		assertNotNull(myBean);
    		assertEquals(1, myBean.getMappers().size());
    		assertTrue(myBean.getMappers().containsKey(Colour.RED));
    		assertEquals(RedMapper.class.getName(), myBean.getMappers().get(Colour.RED).getClass().getName());
    	}
    }
    If you want to try out this code, simply store the XML application context configuration listed at the top of this post in your classpath as com/abc/EnumColourMapperTests-context.xml. Then run the unit test.

    Regards,

    Sam

    Comment


    • #3
      Hi Sam, thanks for the detailed instructions.

      Comment


      • #4
        Is it s spring 3.x option only? I am using 2.0.7.

        Comment


        • #5
          Originally posted by ramat123 View Post
          Is it s spring 3.x option only? I am using 2.0.7.
          The util namespace was introduced in Spring 2.0. See the util schema section of the reference manual for details.

          Regards,

          Sam

          Comment

          Working...
          X