gpt4 book ai didi

java - 使用 Spring 注入(inject)枚举

转载 作者:行者123 更新时间:2023-12-01 16:01:24 26 4
gpt4 key购买 nike

我正在尝试使用 <util:constant 通过 spring 上下文注入(inject) 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>

在这里,我尝试利用 Enum ADSKContentGroup 注入(inject)到我的 bean 的 (ADSKContentGroup) ame 属性中。

这是枚举:

public enum MetadataTypeEnum {
CONTENT_GROUP ("ADSKContentGroup"),

private String metadataType;

private MetadataTypeEnum(String metadataType) {
this.metadataType = metadataType;
}
public String getMetadataType() {
return this.metadataType;
}
}

这是 bean :

public class ADSKContentGroup extends Metadata {
public ADSKContentGroup(){
}
}

bean 从具有名称属性 setter 的基类扩展

这是类定义:

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

不确定我的方法出了什么问题。

任何指针都将受到高度赞赏。

  • 谢谢

最佳答案

您的Metadata类是设计不良的JavaBean,它不符合规范:setter使用MetadataTypeEnum类型的参数,但getter的返回类型是字符串

关于java - 使用 Spring 注入(inject)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3791281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com