Java 如何在 Spring 配置文件中为 bean 的属性分配一个枚举值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/516771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign bean's property an Enum value in Spring config file?
提问by xelurg
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 属性中:
<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?
我应该如何将枚举注入 spring bean?
采纳答案by krosenvold
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!
你试过“TYPE1”吗?我想 Spring 无论如何都使用反射来确定“类型”的类型,因此完全限定名称是多余的。Spring 通常不订阅冗余!
回答by Jacob Mattison
You can just do "TYPE1".
你可以只做“TYPE1”。
回答by Fortyrunner
You can write Bean Editors (details are in the Spring Docs) if you want to add further value and write to custom types.
如果您想添加更多值并写入自定义类型,您可以编写 Bean 编辑器(详细信息在 Spring Docs 中)。
回答by Tsering
Use the value child element instead of the value attribute and specify the Enum class name:
使用 value 子元素代替 value 属性并指定 Enum 类名称:
<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.
这种方法比只写的优点value="ALIEN"
是,如果 Spring 不能从属性推断枚举的实际类型(例如,属性的声明类型是接口),它也可以工作。改编自 araqnid 的评论。
回答by Lucas
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:
我知道这是一个非常古老的问题,但如果有人正在寻找更新的方法来做到这一点,请使用 spring util 命名空间:
<util:constant static-field="my.pkg.types.MyEnumType.TYPE1" />
As described in the spring documentation.
回答by George
This is what did it for me MessageDeliveryMode is the enum the bean will have the value PERSISTENT:
这就是为我做的 MessageDeliveryMode 是 bean 将具有值 PERSISTENT 的枚举:
<bean class="org.springframework.amqp.core.MessageDeliveryMode" factory-method="valueOf">
<constructor-arg value="PERSISTENT" />
</bean>
回答by Mike R
Spring-integration example, routing based on a an Enum field:
Spring 集成示例,基于 Enum 字段的路由:
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>
回答by Paul Rooney
Using SPEL and P-NAMESPACE:
使用 SPEL 和 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}"/>
回答by Yuci
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:
具体来说,将值设置为枚举类型的常量的名称,例如,在您的情况下为“TYPE1”或“TYPE2”,如下所示。它会起作用:
<bean name="someName" class="my.pkg.classes">
<property name="type" value="TYPE1" />
</bean>