【问题标题】:Spring: How do I inject ENUM in Spring configuration?Spring:如何在 Spring 配置中注入 ENUM?
【发布时间】:2012-10-23 13:00:16
【问题描述】:

我有一个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?

我对 Spring 框架非常陌生,不知道如何实现这一点

谢谢

【问题讨论】:

    标签: java spring enums


    【解决方案1】:
     <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>
    

    【讨论】:

      【解决方案2】:

      看来你需要使用“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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 2020-10-04
        • 2018-11-11
        • 1970-01-01
        • 2015-10-25
        • 2020-08-21
        相关资源
        最近更新 更多