我想通過使用<util:constant
通過春季上下文注入Java枚舉。春季注入枚舉
這是我所做的。在我的Spring配置中,我下面的條目
<util:constant id="Content" static-field="com.test.taxonomy.model.MetadataTypeEnum.CONTENT_GROUP" />
<util:constant id="Category" static-field="com.test.taxonomy.model.MetadataTypeEnum.CATEGORY" />
<bean id="ADSKContentGroup" class="com.test.taxonomy.model.ADSKContentGroup" scope="prototype">
<property name="name" ref="Content" />
</bean>
在這裏,我試圖利用一個枚舉ADSKContentGroup在我的bean的(ADSKContentGroup)火焰屬性被注入。
這裏的枚舉:
public enum MetadataTypeEnum {
CONTENT_GROUP ("ADSKContentGroup"),
private String metadataType;
private MetadataTypeEnum(String metadataType) {
this.metadataType = metadataType;
}
public String getMetadataType() {
return this.metadataType;
}
}
這裏的豆:
public class ADSKContentGroup extends Metadata {
public ADSKContentGroup(){
}
}
的豆從具有name屬性
這裏二傳手的基類繼承的類定義:
public class Metadata {
private MetadataTypeEnum name;
private String value;
public String getName() {
return name.getMetadataType();
}
public void setName(MetadataTypeEnum name) {
this.name = name;
}
}
在運行時,我收到以下異常
ERROR com.test.taxonomy.plugin.TaxonomyPluginImpl - Error in creating metadata mapping :Error creating bean with name 'ADSKContentGroup' defined in ServletContext resource [/WEB-INF/beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.test.taxonomy.model.MetadataTypeEnum] to required type [java.lang.String] for property 'name'; nested exception is java.lang.IllegalArgumentException: Original must not be null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ADSKContentGroup' defined in ServletContext resource [/WEB-INF/beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.test.taxonomy.model.MetadataTypeEnum] to required type [java.lang.String] for property 'name'; nested exception is java.lang.IllegalArgumentException: Original must not be null
不知道我的方法出了什麼問題。
任何指針,非常感謝。
- 感謝
什麼版本的java和什麼版本的彈簧 – Woot4Moo 2010-09-24 21:56:00
jdk 1.6,彈簧2.5.5 – Shamik 2010-09-24 22:10:16