2012-10-23 12 views
6

मैं एक ENUM रूपस्प्रिंग: मैं वसंत विन्यास में Enum कैसे इंजेक्षन करते हैं?

package com.myorg.sparrow.s3Environment; 

import javax.annotation.Nonnull; 

public enum DocumentType { 
    Document("document/", ".xml.gz", "binary/octet-stream", "gzip", true); 

    private final String path; 
    private final String suffix; 
    private final String contentType; 
    private final String contentEncoding; 
    private final Boolean compress; 

    private DocumentType(@Nonnull final String path, @Nonnull final String suffix, 
         @Nonnull final String contentType, @Nonnull final String contentEncoding, 
         @Nonnull final Boolean compress) { 
     this.path = path; 
     this.suffix = suffix; 
     this.contentType = contentType; 
     this.contentEncoding = contentEncoding; 
     this.compress = compress; 
    } 

    @Nonnull 
    public String getPath() { 
     return path; 
    } 

    @Nonnull 
    public String getSuffix() { 
     return suffix; 
    } 

    @Nonnull 
    public String getContentType() { 
     return contentType; 
    } 

    @Nonnull 
    public String getContentEncoding() { 
     return contentEncoding; 
    } 

    @Nonnull 
    public Boolean isCompress() { 
     return compress; 
    } 
} 

मैं Spring विन्यास फाइल में DocumentType.Document के इस मूल्य इंजेक्षन करना चाहते हैं

<bean id="s3Service" class="com.myorg.sparrow.business.xml.persist.S3Service"> 
     <constructor-arg ref="awsCredentials" /> 
     <constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here? 
     <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> 
    </bean> 

मैं

<constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here? 

में यह मान कैसे इंजेक्षन कि मैं बहुत हूँ है ढांचा वसंत के कैसे इस

01 प्राप्त करने के लिए नए और यकीन नहीं

धन्यवाद

उत्तर

14
<bean id="s3Service" class="com.myorg.sparrow.business.xml.persist.S3Service"> 
     <constructor-arg ref="awsCredentials" /> 
     <constructor-arg value="Document" /> // We love Spring because it is simpler than we expect 
     <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> 
    </bean> 
+0

बस बहुत बढ़िया! आप @MK धन्यवाद। – daydreamer

0

लगता है जैसे आप "org.springframework.beans.factory.config.FieldRetrievingFactoryBean"

उदाहरण उपयोग करने की आवश्यकता:

<property name="b"> 
    <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> 
    <property name="targetClass" value="whole.package.path.A$B"></property> 
    <property name="targetField" value="FIRST"></property> 
    </bean> 
</property> 

संदर्भ:

http://forum.springsource.org/showthread.php?19396-Accessing-enum-value-defined-in-inner-class

http://forum.springsource.org/showthread.php?19696-Spring-1-2-3-How-do-I-inject-Java-1-5-Enums

संबंधित मुद्दे