Monday, March 28, 2011

How assign bean's property an Enum value in Spring config file?

I have a standalone enum type defined, something like this:

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

No I want to inject a value of that type into a bean property:

<bean name="someName" class="my.pkg.classes">
   <property name="type" value="my.pkg.types.MyEnumType.TYPE1" />
</bean>

...and that didn't work :(

How should I do that?

From stackoverflow
  • Have you tried just "TYPE1" ? I suppose spring uses reflection to determine the type of "type" anyway, so the fully qualified name seems redundant. I must admit I'm guessing, but spring generally doesn't subscribe to redundancy ;)

  • You can just do "TYPE1".

  • You can write Bean Editors (details are in the Spring Docs) if you want to add further value and write to custom types.

  • Thanks a lot Guys!! You are awesome..

  • Use the value child element instead of the value attribute and specify the Enum class name:

    <property name="residence">
    <value type="SocialSecurity$Residence">ALIEN</value>
    </property>
    

0 comments:

Post a Comment