114

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

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

Now, 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 Inject an Enum into a spring bean?

xelurg
  • 4,294
  • 9
  • 35
  • 37

10 Answers10

133

Have you tried just "TYPE1"? I suppose Spring uses reflection to determine the type of "type" anyway, so the fully qualified name is redundant. Spring generally doesn't subscribe to redundancy!

Michael
  • 41,989
  • 11
  • 82
  • 128
krosenvold
  • 75,535
  • 32
  • 152
  • 208
46

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>

The advantage of this approach over just writing value="ALIEN" is that it also works if Spring can't infer the actual type of the enum from the property (e.g. the property's declared type is an interface).Adapted from araqnid's comment.

sleske
  • 81,358
  • 34
  • 189
  • 227
Tsering
  • 559
  • 4
  • 2
35

I know this is a really old question, but in case someone is looking for the newer way to do this, use the spring util namespace:

<util:constant static-field="my.pkg.types.MyEnumType.TYPE1" />

As described in the spring documentation.

Lucas
  • 14,227
  • 9
  • 74
  • 124
12

You can just do "TYPE1".

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
6

This is what did it for me MessageDeliveryMode is the enum the bean will have the value PERSISTENT:

<bean class="org.springframework.amqp.core.MessageDeliveryMode" factory-method="valueOf">
    <constructor-arg value="PERSISTENT" />
</bean>
George
  • 309
  • 3
  • 6
5

Using SPEL and P-NAMESPACE:

<beans...
xmlns:p="http://www.springframework.org/schema/p" ...>
..
<bean name="someName" class="my.pkg.classes"
    p:type="#{T(my.pkg.types.MyEnumType).TYPE1}"/>
Paul Rooney
  • 111
  • 1
  • 5
1

To be specific, set the value to be the name of a constant of the enum type, e.g., "TYPE1" or "TYPE2" in your case, as shown below. And it will work:

<bean name="someName" class="my.pkg.classes">
   <property name="type" value="TYPE1" />
</bean>
Yuci
  • 27,235
  • 10
  • 114
  • 113
0

Spring-integration example, routing based on a an Enum field:

public class BookOrder {

    public enum OrderType { DELIVERY, PICKUP } //enum
    public BookOrder(..., OrderType orderType) //orderType
    ...

config:

<router expression="payload.orderType" input-channel="processOrder">
    <mapping value="DELIVERY" channel="delivery"/>
    <mapping value="PICKUP" channel="pickup"/>
</router>
Mike R
  • 4,448
  • 3
  • 33
  • 40
0

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

Fortyrunner
  • 12,702
  • 4
  • 31
  • 54
0

If you have enum:

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

You may instantiate it as a bean within this xml config

<bean name="someName" class="my.pkg.types.MyEnumType" factory-method="valueOf">
    <constructor-arg value="TYPE1"/>
</bean>
exploder86
  • 51
  • 4