java Spring:如何在 Spring 配置中注入 ENUM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13030974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:14:29  来源:igfitidea点击:

Spring: How do I inject ENUM in Spring configuration?

javaspringenums

提问by daydreamer

I have a ENUMas

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

I want to inject this value of DocumentType.Documentin Springconfiguration file

我想DocumentType.DocumentSpring配置文件中注入这个值

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

How do I inject this value in

我如何注入这个值

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

I am very new to Spring framework and not sure how to achieve this

我对 Spring 框架很陌生,不知道如何实现

Thank you

谢谢

回答by MK.

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

回答by Ravi K

Seems like you need to use "org.springframework.beans.factory.config.FieldRetrievingFactoryBean"

似乎您需要使用“org.springframework.beans.factory.config.FieldRetrievingFactoryBean”

example:

例子:

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

References:

参考:

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

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

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