如何在Spring配置文件中为bean的属性分配一个Enum值? [英] How to assign bean's property an Enum value in Spring config file?

查看:543
本文介绍了如何在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!

这篇关于如何在Spring配置文件中为bean的属性分配一个Enum值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆