baliman
baliman

Reputation: 620

Setting environment for property location

I am configuring my spring application using property file. But I have to make a switch between development and production property file. Current I have this code snippet

@Configuration
@PropertySource(value = "classpath:config/simulator.properties", ignoreResourceNotFound = false)
public class AppConfiguration

But I would like something with value = "classpath:${env:local}/simulator.properties"

which means if I not set the environment variable env than it must point to local/simulator.properties else if environment env variable points to production the location must be production/simulator.properties.

So, the local is the fallback environment.

Is there any way to achieve this. I do not want to use profiles, it must be controlled by an environment variable

I do not want to set a -D option for profiles

Thanks

Johan

Upvotes: 1

Views: 148

Answers (3)

Michael Gantman
Michael Gantman

Reputation: 7790

Actually in my Tests (Unit Test 4) I used annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:com/myTest/MyTestApplicationContext.xml"})

This allows you to have a separate Application context for each test and separate one for development environment. Then of course in each of your Application contexts you can configure properties to be read from different place. Works great for me

Upvotes: 0

Gopinath
Gopinath

Reputation: 19

You can use multiple @PropertySource annotations, if the first file and the second file are found, and the keys in both the file matches then the later one will be taken. Please have a look at here

@PropertySource(value="classpath:local/simulator.properties",ignoreResourceNotFound=true)
@PropertySource(value="classpath:${env.production}/simulator.properties",ignoreResourceNotFound=true)

Upvotes: 1

Arvinder Chhabra
Arvinder Chhabra

Reputation: 46

Spring automatically look at from system root path if we have windows then c:/ will be automatically understood by spring and if we have Linux machine then / will be root. So here we don't need to set classpath or -D tags.

Upvotes: 0

Related Questions