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
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.
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.
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 .
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.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With