2010-09-24 9 views
0

J'essaie d'injecter java enum par le biais d'un contexte de ressort en utilisant <util:constant.Injection d'une énumération avec Spring

Voici ce que j'ai fait. Dans ma config de printemps, je suis entrée suivante

<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> 

Ici, je suis en train de tirer parti d'un Enum ADSKContentGroup à injecter dans mon haricot (ADSKContentGroup) propriété amme.

Voici le ENUM:

public enum MetadataTypeEnum { 
    CONTENT_GROUP ("ADSKContentGroup"), 

    private String metadataType; 

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

Voici le haricot:

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

Le haricot s'étend d'une classe de base qui a le setter pour attribut name

est ici la définition de classe:

public class Metadata { 
    private MetadataTypeEnum name; 
    private String value; 

    public String getName() { 
    return name.getMetadataType(); 
    } 

    public void setName(MetadataTypeEnum name) { 
    this.name = name; 
    } 
} 

Dans l'exécution, je commence à l'exception suivante

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 

Je ne sais pas ce qui ne va pas avec mon approche.

Tout pointeur est très apprécié.

  • Merci
+0

quelle version de java et quelle version du ressort – Woot4Moo

+0

jdk 1.6, ressort 2.5.5 – Shamik

Répondre

1

Votre Metadata classe est JavaBean mal conçu, il ne se conforme pas aux spécifications: Le poseur utilise un paramètre de type MetadataTypeEnum, mais le type de retour du getter est String.

+0

Vous avez raison, merci pour votre réponse ... – Shamik

Questions connexes