Home > Software engineering >  How to combine two environment variable in @Value annotation
How to combine two environment variable in @Value annotation

Time:06-30

I have application.yaml as following

environment: ${ZK_ENVIRONMENT}
END_POINT_URL:
  dev: http://sampledev.uk.com
  qa: http://sampleqa.uk.com
  prod: http://sampleprod.uk.com

environment values can be dev,qa or prod. I need o inject END_POINT_URL based on environment in @Value.Tried the following, it's not working.

@Value("${END_POINT_URL}.${environment}")
private String url;

CodePudding user response:

This should work, just did a quick test as well

@Value("${END_POINT_URL.${environment}}")

@SpringBootApplication
public class DemoApplication {
    
    @Value("${END_POINT_URL.${environment}}")
    private String value;
    
    @PostConstruct
    public void init() {
        System.out.println(value);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

ZK_ENVIRONMENT:dev Output : http://sampledev.uk.com

  • Related