@PropertySource working with spel (systemEnvironment and systemProperties)

1.8k views Asked by At

I have in src/main/resources the following files

bpp-dev.properties
bpp-prod.properties
bpp-test.properties

Through my STS I can define the key envB, it in two places

  1. how a VM argument such as -DenvB=dev
  2. how an Environment such as Variable envB and Value prod

If in a Configuration class I have the following.

@Configuration
@PropertySource("classpath:/com/manuel/jordan/properties/bpp-${envB}.properties")
public class PropertiesConfiguration {

It works fine but always has preference the System Properties over Environment Variables, it is the default behaviour. I have no problem here.

But if I want work explicitly with Environment Variables, the following fails

@Configuration
@PropertySource("classpath:/com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties")
public class PropertiesConfiguration {

Always I receive:

Caused by: java.io.FileNotFoundException: 
class path resource 
[com/manuel/jordan/properties/bpp-#{systemEnvironment['envB']}.properties]
cannot be opened because it does not exist

How I can fix this?

If I use the functional @PropertySource and just playing in the same @Configuration class I work with the following:

@Value("#{systemProperties['envB']}")
private String propertiesEnvB;
@Value("#{systemEnvironment['envB']}")
private String environmentEnvB;

to be printed later, both works fine.

0

There are 0 answers