I ran into this for property substitution in spring
<context:property-placeholder location="esb-project-config.properties"/>
but unfortunately, we don't want this in the xml file as we want to re-use the file in our tests but swap in the test.properties file for test. ie. we want to test all the production bindings but with properties that are suitable for test like localhost for instance. How can we load the ApplicationContext but with different properties files?
thanks,
Dean
Best Answer
several approaches:
1. 'Order' Property
in
src/main/resources/your-conf.xml
in
src/test/resources/your-test-config.xml
If you running your test with
src/test/resources
as a test classpath, the above will ensure to overridesrc/main/resources/esb-project-config.properties
with thesrc/test/resources/esb-project-config.properties
.This will override the whole
property-placeholder
though, so you would have to provide all the properties needed in your application in for this testproperty-placeholder
. e.g.2. PropertyOverrideConfigurer
to override certain individual properties. Some examples here
3. System Variables
You can use a prefix to control environment specific properties, this can be done by using system variables:
In this case it will always look under:
by default, unless a
ENV_SYSTEM
system variable is set. If it is set toqa
, for example, it will automatically look under:4. Spring Profiles
Another approach is to make beans profile specific. For example:
The appropriate
esb-project-config
will loaded depending on a profile set. For example this will loadesb-project-config.dev.properties
: