如何使用Spring获取Enum的值
<bean id="xyz" class="com.abc" >
<property name="name">
<bean
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.abc.staticname" />
</bean>
</property>
</bean>
这是我之前设置类com.abc的名称的方法。现在,名称应该来自另一个枚举。如何访问枚举值来设置类 com.abc 的 name 属性?
<bean id="xyz" class="com.abc" >
<property name="name">
<bean
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.abc.staticname" />
</bean>
</property>
</bean>
This is the way previously I used to set the name of class com.abc. Now, the names should come from another enum. How do I access the enum value to set the name property of my class com.abc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白为什么你不能继续使用 FieldRetreeringFactoryBean,这就是它的用途。
不过,它比您的示例建议的更容易使用。此外,还有更简单的基于模式的语法,它可以执行相同的操作,
。这两种方法都有记录(并进行了比较)此处。
(请记住,枚举值只是枚举类上的静态字段)
I don't see why you can't keep on using FieldRetrievingFactoryBean, that's what it's for.
It's a little bit easier to use than your example suggests, though. Also, there's the easier schema-based syntax which does the same thing,
<util:constant>
.Both approaches are documented (and compared) here.
(Remember that enum values are just static fields on the enum class)
您可以只使用枚举名称作为值,Spring 会自动检测到它是枚举类型的静态字段并使用它。例如,如果您有一个枚举 com.mycompany.MyEnum,其值为 SOMEVAL、ANOTHERVAL,您可以使用 :
这将允许您完全避免 FieldRetrievingFactoryBean 和
。You can just use the enum name as the value, and Spring will automatically detect that it's a static field of the enum type and use it. So for example, if you have an enum com.mycompany.MyEnum with values SOMEVAL, ANOTHERVAL, you can use :
This will allow you to avoid FieldRetrievingFactoryBean and
<util:constant>
altogether.