0

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

    @Value("#{systemEnvironment['AWS_ENV']}")
    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:

@PropertySource("classpath:secrets-${#{systemEnvironment['AWS_ENV']}.properties")

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

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
Toofy
  • 804
  • 9
  • 19
  • See Spring profiles, or have a single aws.properties file and move the placeholder to inside the properties file. – Andrew S Aug 25 '20 at 12:02
  • can you reference system environment variables from within a properties file? I'm not sure that will even work because I'm using the PropertySource annotation to load the property file in the first place – Toofy Aug 25 '20 at 12:06
  • Does this answer your question? [Java Spring: How to use `@Value` annotation to inject an `Environment` property?](https://stackoverflow.com/questions/14617181/java-spring-how-to-use-value-annotation-to-inject-an-environment-property) – Giorgi Tsiklauri Aug 25 '20 at 12:18

2 Answers2

0

The placeholder could be moved to a single aws.properties file:

aws.properties 
envTarget = ${AWS_ENV}

then:

@PropertySource("classpath:aws.properties")

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 aws-local.properties (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.

Andrew S
  • 2,509
  • 1
  • 12
  • 14
0

While the accepted answer did partially solve my problem I ended up just manually configuring the Properties without using the PropertySource annotation. This allowed me to have better control over the bean lifecycles and the property file to load.

Spring Environment Property Source Configuration

Toofy
  • 804
  • 9
  • 19