Rigo Sarmiento
Rigo Sarmiento

Reputation: 493

How do you assign a non string value (enum/class) to application.properties?

I'm using AWS in my project and I just want to make Regions configurable in properties file.

So com.amazonaws.regions.Regions accepts enum values like Regions.AP_EAST_1.

I tried assigning it in application.properties:

aws.s3.config.region=com.amazonaws.regions.Regions.US_WEST_2
@Value("${aws.s3.config.region}")
private Regions clientRegion;

This just doesn't work out. It gives me this:

java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.amazonaws.regions.Regions]: no matching editors or conversion strategy found

Upvotes: 0

Views: 317

Answers (1)

Spring knows how to map enums. Just use the enum value (not a fully-qualified name):

aws.s3.config.region: US_WEST_2

Note also that Spring Cloud AWS is available (though it may not always be the best choice; it has very strong opinions), and that in general it's better to use @ConfigurationProperties or at a minimum constructor injection instead of @Value on a field, which has all the pitfalls of field injection.

Upvotes: 2

Related Questions