8

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 ?

USer22999299
  • 5,284
  • 9
  • 46
  • 78

2 Answers2

18

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.

USer22999299
  • 5,284
  • 9
  • 46
  • 78
4

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

reos
  • 8,766
  • 6
  • 28
  • 34
  • after i'm using the above profile technic, how should i map the xxx.sandbox or xxx.production according to the profile from the application.properites file? – USer22999299 May 13 '16 at 18:58
  • What kind of application are you running ? You can use this property -Dspring.profiles.active=production" at the moment you run the application – reos May 13 '16 at 19:13
  • OK, but these profile can help you to map the beans for spring, how i can use it to map in case of profile x use xxx.production, in case of profile y use xxx.sandbox? – USer22999299 May 13 '16 at 19:20
  • 1
    You can use something like this at the end, thats depends on how are you reading the properties – reos May 13 '16 at 19:51
  • Thanks! it helps a lot! – USer22999299 May 14 '16 at 08:53