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

前端 未结 9 888
遥遥无期
遥遥无期 2020-11-28 06:16

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

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

Now, I w

相关标签:
9条回答
  • 2020-11-28 06:37

    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>
    
    0 讨论(0)
  • 2020-11-28 06:40

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

    0 讨论(0)
  • 2020-11-28 06:41

    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>
    
    0 讨论(0)
提交回复
热议问题