USer22999299
USer22999299

Reputation: 5694

Using @Value with condition in spring in order to map value to string

I have a application.properites file with the following:

xxx.xxx = sandbox
xxx.sandbox = 123
xxx.production = 456

I would like to map to a string value 123 in case xxx.xxx == sandbox and 456 in case xxx.xxx == production

...
public class temp { 

 @Value("${??????}")
    private String token;
}

is it possible to fill in a condition incited of the ?????? that will map the token to 123 or 456 according to xxx.xxx ?

Upvotes: 9

Views: 16049

Answers (2)

USer22999299
USer22999299

Reputation: 5694

A simple way in case someone will hit this question:

@Value("#{'${xxx.xxx}'=='sandbox' ? '${xxx.sandbox}' : '${xxx.production}'}")

I just think it is much easier that start working with profiles.

Upvotes: 19

reos
reos

Reputation: 8334

You can use Spring Profiles, so you can have a property file for each enviroment.

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded

You can see more here http://www.baeldung.com/spring-profiles

http://www.mkyong.com/spring/spring-profiles-example/

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

Upvotes: 4

Related Questions