Loading property file from system properties with default path in Spring context -
i trying load properties file (located outside of war) in spring context path coming system properties.
if system property doesn't exist or path not found, i'd fallback default properties file included in .war.
here specific part of applicationcontext.xml
<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true" location="file:${config.dir}/config/server.properties"/> <context:property-placeholder ignore-resource-not-found="false" location="classpath:config/server.properties"/>
problem when config.dir isn't found in system properties, exception thrown saying resolver failed find property.
and case resolve, i'm pretty sure second line make properties loaded in file given in parameter replaced 1 in default file, opposite of want do.
i'm using spring 4.x xml configuration.
is possible want ? i'm aware of @conditional java based configuration i'm able use xml way respond criteria of project.
don't use 2 placeholders, use single one, location
property takes ,
separated list of files load.
<context:property-placeholder ignore-resource-not-found="true" location="file:${config.dir}/config/server.properties,classpath:config/server.properties"/>
however config.dir
property has available else loading blow up.
another solution use applicationcontextinitializer
, depending on availability of config.dir
property load or not load additional file.
public class configinitializer implements applicationcontextinitializer { public void initialize(configurableapplicationcontext applicationcontext) { configurableenvironment env = applicationcontext.getenvironment(); mutablepropertysources mps = env.getpropertysources(); mps.addlast(new resourcepropertysource("server.properties", "classpath:config/server.properties")); if (env.containsproperty("config.dir")) { string configfile = env.getproperty("config.dir")+"/config/server.properties"; resource resource = applicationcontext.getresource(configfile); if (resource.exists() ) { mps.addbefore("server.properties", new resourcepropertysource(resource)); } } } }
now need empty <context:property-placeholder />
element.
added advantage specify default config.dir
in default properties , have overridden system or environment property.
Comments
Post a Comment