Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: How do I inject ENUM in Spring configuration?

Tags:

java

enums

spring

I have a ENUM as

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.Document in Spring configuration file

   <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

Thank you

like image 950
daydreamer Avatar asked Oct 23 '12 13:10

daydreamer


People also ask

Does Spring support injection of enum types?

There are many ways to inject the enum object in the spring framework. the second approach is to inject using factory-method as in the below screenshot. The above approach is lightweight and the spring container validates the configuration when the container is started.

How do you declare an enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.

Where should spring configuration be placed?

In essence the Spring configuration files (that can have any name by the way, not just the generic applicationContext. xml ) are treated as classpath resources and filed under src/main/resources .

What is java enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


1 Answers

 <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>
like image 139
MK. Avatar answered Nov 14 '22 21:11

MK.