在Spring上下文中使用默认路径从系统属性加载属性文件 [英] Loading property file from system properties with default path in Spring context

查看:180
本文介绍了在Spring上下文中使用默认路径从系统属性加载属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring上下文中加载属性文件(位于战争之外),并使用来自系统属性的路径.

I am trying to load a properties file (located outside of the war) in my Spring context with a path coming from the system properties.

如果该系统属性不存在或找不到路径,我想回退到.war中包含的默认属性文件.

If that system property doesn't exist or the path is not found, I'd like to fallback to a default properties file included in my .war.

这是我的applicationContext.xml的特定部分

Here is the specific part of my 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"/>

问题是,当在系统属性中找不到config.dir时,会引发异常,说明解析器无法找到该属性.

Problem is that when config.dir isn't found in the system properties, an Exception is thrown saying that the resolver failed to find that property.

即使它可以解决,我也很确定第二行将使参数给定的文件中加载的属性替换为默认文件中的属性,这与我的相反想做.

And even the case it would resolve, I'm pretty sure that the second line would make so that the properties loaded in the file given in parameter are replaced by the one in the default file, which is the opposite of what I want to do.

我正在使用Spring 4.x和仅xml配置.

I'm using Spring 4.x with xml only configuration.

可以做我想做的事吗? 我知道@Conditional用于基于Java的配置,但是我只能使用xml方式来响应项目的条件.

Is it possible to do what I want ? I'm aware of the @Conditional for a Java based configuration but I'm only able to use the xml way to respond to the criteria of the project.

推荐答案

不要使用2个占位符,只使用一个占位符,location属性采用,分隔的文件列表进行加载.

Don't use 2 placeholders, use a single one, the location property takes a , separated list of files to load.

<context:property-placeholder ignore-resource-not-found="true" location="file:${config.dir}/config/server.properties,classpath:config/server.properties"/>

但是config.dir属性必须可用,否则负载将爆炸.

However the config.dirproperty has to be available else the loading will blow up.

另一种解决方案是使用

Another solution would be to use an ApplicationContextInitializer and depending on the availability of the config.dir property load or not to load the 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));
            }
        }
    }
}

现在您只需要一个空的<context:property-placeholder />元素.

Now you only need an empty <context:property-placeholder /> element.

附加的优点是,您可以在默认属性中指定默认的config.dir,并将其替换为系统或环境属性.

Added advantage is that you could specify a default config.dir in your default properties and have that overridden by a system or environment property.

这篇关于在Spring上下文中使用默认路径从系统属性加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆