Gug*_*see 5 java enums spring javabeans
当从Eclipse调用Spring的"Validate"时,当我想使用Enum的隐式"valueOf"方法返回枚举时,我会遇到很多错误.
例如:
<bean id="docFamily" class="...DocFamily" factory-method="valueOf">
<constructor-arg>
<value>LOGY</value>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
有Eclipse告诉我:
工厂bean类中找不到1个参数的非静态工厂方法'valueOf'...
但是据我从文档中了解到:
BeanWrapperImpl支持JDK 1.5枚举和旧式枚举类:字符串值将被视为枚举值名称
所以上面应该正常吗?(在这种情况下,btw是'constructor-arg'正确的标签,不应该是'method-arg'吗?).
为什么Eclipse/Spring的"Validate"会给我错误信息?
Enum.valueOf()有两个参数:
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
Run Code Online (Sandbox Code Playgroud)
因此,所需的定义可能如下所示:
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
Run Code Online (Sandbox Code Playgroud)
然而,像这样的东西可能是一个更优雅的解决方案:
<bean id="docFamily" class="java.lang.Enum" factory-method="valueOf">
<constructor-arg index = "0"><value>...DocFamily</value></constructor-arg>
<constructor-arg index = "1"><value>LOGY</value></constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4667 次 |
| 最近记录: |