Stefan
Stefan

Reputation: 12453

System property suffix for use in PropertyPlaceholderConfigurer

I having problems defining database properties in different environments. The properties file looks like this:

db.url-DEV=host1:port:con...
db.user-DEV=user1

db.url-PROD=host2:port:con...
db.user-PROD=user2

The suffixes (-DEV,-PROD) are set on server side using a system property. How may I configure my PropertyPlaceholderConfigurer to check for a system property to switch to the right configuration? There has to be a default value if a system property is not set.

I have tried something like this, but that did not work:

${#{'db.url'+${systemEnvironmentVar}}}

The PropertyConfigurer says 'property with name 'db.url'+${systemEnvironmentVar} not found.

Upvotes: 0

Views: 299

Answers (2)

yname
yname

Reputation: 2245

Simply use nested expression, example from real project:

<property name="username" value="${${ENV_NAME}database.username}" />
<property name="password" value="${${ENV_NAME}database.password}" /> 

where ENV_NAME is system property. Note that you have to declare this variable in jvm parameters. You can also add default ENV_NAME to your properties:

ENV_NAME=dev

And configure propertyPlaceholder to override with system properties:

<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

more about this here

But you can also use spring profiles

Upvotes: 1

MariuszS
MariuszS

Reputation: 31577

Create two properties files dev.properties and prod.properties with content

db.url=host1:port:con...
db.user=user1

This is common Spring pattern.

Upvotes: 0

Related Questions