Inject system environment variable into Spring Annotation with placeholder

Tags: ,

I know you can inject environment variables with the @Value annotation like this

    private String envTarget;

If I am using a Spring annotation however can I inject the environment variable in-line into the String value? For example something like this:


Obviously the above doesn’t work as it tries to resolve systemEnvironment['AWS_ENV'] as a jvm property. Anyone have any ideas?


The placeholder could be moved to a single file: 
envTarget = ${AWS_ENV}



For local development, the placeholder can be added as JVM parameters in the run configuration, but that can become a pain to manage. An alternative would be to have a (located in same resources folder), but this file is in .gitignore so secrets are never committed. Then there is a single JVM parameter to use the local profile.

Source: stackoverflow