如何在Spring配置文件中为bean的属性分配一个Enum值?

问题描述:

我定义了一个独立的枚举类型,如下所示:

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

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

现在,我想在bean属性中注入该类型的值:

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?

你试过TYPE1吗?我假设Spring使用反射来确定类型的类型,所以完全限定的名称是多余的.Spring一般不订阅冗余!

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!