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

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

问题描述

在我的 Spring xml 配置中,我试图让这样的事情起作用:

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>

我想根据属性文件中的属性决定要导入的文件.我知道我可以使用系统属性,但我无法在启动时向 JVM 添加属性.

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.

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

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...

谢谢

推荐答案

不幸的是,这比应有的困难得多.在我的应用程序中,我通过执行以下操作来完成此操作:

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

  1. 一个小的引导"上下文,负责加载 PropertyPlaceholderConfigurer bean 和另一个负责引导应用程序上下文的 bean.

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

上面提到的第二个 bean 将要加载的真实"spring 上下文文件作为输入.我已经组织了我的 spring 上下文文件,以便可配置部分是众所周知的并且在同一个地方.例如,我可能有 3 个配置文件:one.onpremise.xml、one.hosted.xml、one.multitenant.xml.bean 以编程方式将这些上下文文件加载到当前应用程序上下文中.

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.

这是有效的,因为上下文文件被指定为负责加载它们的 bean 的输入.正如您所提到的,如果您只是尝试进行导入,则它不会起作用,但这具有相同的效果,但需要做更多的工作.引导类看起来像这样:

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.

希望这会有所帮助.

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

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