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 ?
Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.
no :) you can use annotations to annotate classes, fields, methods and their arguments. but not in methods, since there is no way, to get method- local variables using reflection in order to process these annotations. Use @Value in your field, and read the value from your method.
How to Inject a Map From a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.
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.
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