Using Enums with map in Spring

If you want to use enums as a key in a map in spring, you can use the following code in your spring.xml:

<bean id="fileCopyService" class="my.package.FileCopyService">
<property name="fileCopier">
<entry key="IMAGE">
<ref bean="imageFileCopier"/>
<entry key="VIDEO">
<ref bean="videoFileCopier"/>
<entry key="AUDIO">
<ref bean="audioFileCopier"/>

<bean id="imageFileCopier" class="my.package.ImageFileCopier"/>
<bean id="videoFileCopier" class="my.package.VideoFileCopier"/>
<bean id="audioFileCopier" class="my.package.AudioFileCopier"/>

and your FileCopyService class is as follows:

private Map<ContentType, FileCopier> fileCopier = new HashMap<ContentType, FileCopier>();

and that is all. So you can access a fileCopier withing the code with :


no if – else statements, or long switches.

One thought on “Using Enums with map in Spring

  1. i look your example to setting.But i have an error .
    Failed to convert property value of type ‘java.util.LinkedHashMap’ to required type ‘java.util.Map’ for property


    can you tell me how to slove the question??


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s