基于.properties文件中的属性导入Spring配置文件 [英] Import Spring config file based on property in .properties file

查看:143
本文介绍了基于.properties文件中的属性导入Spring配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring xml配置我试图得到这样的工作:

 < beans& 

< import resource =$ {file.to.import}/>

<! - 其他bean定义 - >

< / beans>

我想基于属性文件中的属性来决定要导入哪个文件。
我知道我可以使用系统属性,但是我不能在启动时添加一个属性到JVM。



注意:PropertyPlaceHolderConfigurer会工作。导入在运行任何BeanFactoryPostProcessors之前解析。 import元素只能解析System.properties。



有没有人有一个简单的解决方案?我不想开始子类化框架类等等...



感谢

解决方案

不幸的是,这是比它应该更难的。在我的应用程序中,我通过执行以下操作来完成此操作:


  1. 一个小的bootstrap上下文,负责加载PropertyPlaceholderConfigurer


  2. 上面提到的第二个bean作为输入,要加载的真正的spring上下文文件。我有我的春天上下文文件组织,使可配置的部分是众所周知的,在同一个地方。例如,我可能有3个配置文件:one.onpremise.xml,one.hosted.xml,one.multitenant.xml。


这是因为上下文文件被指定为输入该bean负责加载它们。它不会工作,如果你只是尝试做一个导入,如你所提到的,但这有相同的效果与稍微更多的工作。引导类看起来像这样:

  public class Bootstrapper实现ApplicationContextAware,InitializingBean {

private WebApplicationContext上下文;
private String [] configLocations;
private String [] testConfigLocations;
private boolean loadTestConfigurations;

public void setConfigLocations(final String [] configLocations){
this.configLocations = configLocations;
}

public void setTestConfigLocations(final String [] testConfigLocations){
this.testConfigLocations = testConfigLocations;
}

public void setLoadTestConfigurations(final boolean loadTestConfigurations){
this.loadTestConfigurations = loadTestConfigurations;
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext)throws BeansException {
context =(WebApplicationContext)applicationContext;
}

@Override
public void afterPropertiesSet()throws Exception {
String [] configsToLoad = configLocations;

if(loadTestConfigurations){
configsToLoad = new String [configLocations.length + testConfigLocations.length];
arraycopy(configLocations,0,configsToLoad,0,configLocations.length);
arraycopy(testConfigLocations,0,configsToLoad,configLocations.length,testConfigLocations.length);
}

context.setConfigLocations(configsToLoad);
context.refresh();
}
}

基本上,获取应用程序上下文, ,并让它刷新自己。这在我的应用程序完美。



希望这有助于。


In my Spring xml configuration I'm trying to get something like this to work:

<beans>

   <import resource="${file.to.import}" />

   <!-- Other bean definitions -->

</beans>

I want to decide which file to import based on a property in a properties file. I know that I can use a System property, but I can't add a property to the JVM at startup.

Note: The PropertyPlaceHolderConfigurer will not work. Imports are resolved before any BeanFactoryPostProcessors are run. The import element can only resolve System.properties.

Does anyone have a simple solution to this? I don't want to start subclassing framework classes and so on...

Thanks

解决方案

This is, unfortunately, a lot harder than it should be. In my application I accomplished this by doing the following:

  1. A small, "bootstrap" context that is responsible for loading a PropertyPlaceholderConfigurer bean and another bean that is responsible for bootstrapping the application context.

  2. The 2nd bean mentioned above takes as input the "real" spring context files to load. I have my spring context files organized so that the configurable part is well known and in the same place. For example, I might have 3 config files: one.onpremise.xml, one.hosted.xml, one.multitenant.xml. The bean programmatically loads these context files into the current application context.

This works because the context files are specified as input the the bean responsible for loading them. It won't work if you just try to do an import, as you mentioned, but this has the same effect with slightly more work. The bootstrap class looks something like this:

 public class Bootstrapper implements ApplicationContextAware, InitializingBean {

    private WebApplicationContext context;
    private String[] configLocations;
    private String[] testConfigLocations;
    private boolean loadTestConfigurations;

    public void setConfigLocations(final String[] configLocations) {
        this.configLocations = configLocations;
    }

    public void setTestConfigLocations(final String[] testConfigLocations) {
        this.testConfigLocations = testConfigLocations;
    }

    public void setLoadTestConfigurations(final boolean loadTestConfigurations) {
        this.loadTestConfigurations = loadTestConfigurations;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        context = (WebApplicationContext) applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] configsToLoad = configLocations;

        if (loadTestConfigurations) {
            configsToLoad = new String[configLocations.length + testConfigLocations.length];
            arraycopy(configLocations, 0, configsToLoad, 0, configLocations.length);
            arraycopy(testConfigLocations, 0, configsToLoad, configLocations.length, testConfigLocations.length);
        }

        context.setConfigLocations(configsToLoad);
        context.refresh();
    }
}

Basically, get the application context, set its config locations, and tell it to refresh itself. This works perfectly in my application.

Hope this helps.

这篇关于基于.properties文件中的属性导入Spring配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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