farvilain
farvilain

Reputation: 2562

Overriding properties with System in Spring

I develop a rest WS that will run on AWS BeanStalk.

For the moment, the datasource is configured with property files:

database.driverClass=org.postgresql.Driver
database.jdbcUrl=jdbc:postgresql://localhost:5432/public
database.username=postgres
database.password=postgres

And in context.xml:

<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
    <property name="driverClass" value="${database.driverClass}" />
    <property name="jdbcUrl" value="${database.jdbcUrl}"/>
....

But now, I need to have a preprod environnement, using AWS Beanstalk, that expose my system properties like RDS_HOSTNAME, RDS_PORT, RDS_DB_NAME, ...

Is there a way to keep the same system, like writing

database.jdbcUrl=jdbc:postgresql://#{RDS_HOSTNAME}:#{RDS_PORT}/#{RDS_DB_NAME}

In preprod.property?

Or to reset database.jdbcUrl with system property in context.xml?

Upvotes: 1

Views: 687

Answers (1)

sergiu
sergiu

Reputation: 389

You could do

   <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" location="classpath:database.properties, file:preProd.properties" />

And preProd.properties will be on the preprod machine. The keys will be the same but the values will be different. This way if preProd.properties is not found (on dev machine for example) the database.properties will be used. If the file preProd.properties is present it will override the values from database.properties.

If you are using maven you could also use maven profile and maven-replacer-plugin.

Upvotes: 4

Related Questions