在多个项目/模块中使用多个属性文件(通过PropertyPlaceholderConfigurer) [英] Using multiple property files (via PropertyPlaceholderConfigurer) in multiple projects/modules

查看:102
本文介绍了在多个项目/模块中使用多个属性文件(通过PropertyPlaceholderConfigurer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在编写一个分为多个项目/模块的应用程序。例如,让我们采用以下模块:

We are currently writing an application which is split into multiple projects/modules. For example, let's take the following modules:


  • myApp-DAO

  • myApp-jabber

每个模块都有自己的Spring上下文xml文件。对于DAO模块,我有一个PropertyPlaceholderConfigurer,它使用必要的db连接参数读取属性文件。在jabber模块中,我还有一个用于jabber连接属性的PropertyPlaceHolderConfigurer。

Each module has its own Spring context xml file. For the DAO module I have a PropertyPlaceholderConfigurer which reads a property file with the necessary db connection parameters. In the jabber module I also have a PropertyPlaceHolderConfigurer for the jabber connection properties.

现在主要的应用程序包括myApp-DAO和myApp-jabber。它读取所有上下文文件并启动一个大的Spring上下文。不幸的是,似乎每个上下文只能有一个PropertyPlaceholderConfigurer,因此首先加载的模块能够读取它的连接参数。另一个引发异常,出现错误,例如无法解决占位符'jabber.host'

Now comes the main application which includes myApp-DAO and myApp-jabber. It reads all the context files and starts one big Spring context. Unfortunately it seems like there can only be one PropertyPlaceholderConfigurer per context, so whichever module gets loaded first is able to read it's connection parameters. The other one throws an exception with an error like "Could not resolve placeholder 'jabber.host'"

我有点明白问题是什么,但我不知道真的知道一个解决方案 - 或者我的用例的最佳实践。

I kind of understand what the problem is, but I don't really know a solution - or the best practice for my usecase.

我如何配置每个模块,以便每个模块都能加载自己的属性文件?现在我已经将PropertyPlaceHolderConfigurer移出了单独的上下文文件,并将它们合并到主应用程序的上下文中(使用单个PropertyPlaceHolderConfigurer加载所有属性文件)。这很糟糕,因为现在每个使用dao模块的人都必须知道,他们在上下文中需要一个PropertyPlaceHolderConfigurer .. dao模块中的集成测试也会失败等。

How would I configure each module so that each one is able to load its own property file? Right now I've moved the PropertyPlaceHolderConfigurer out of the seperate context files and merged them into the main application's context (loading all property files with a single PropertyPlaceHolderConfigurer). This sucks though, because now everyone who uses the dao module has to know, that they need a PropertyPlaceHolderConfigurer in their context .. also the integration tests in the dao module fail etc.

我很想知道来自stackoverflow社区的解决方案/想法..

I'm curious to hear about solutions/ideas from the stackoverflow community..

推荐答案

如果你确保每个地方持有人在所涉及的每个上下文中,忽略了无法解析的密钥,然后这两种方法都起作用。例如:

If you ensure that every place holder, in each of the contexts involved, is ignoring unresolvable keys then both of these approaches work. For example:

<context:property-placeholder
location="classpath:dao.properties,
          classpath:services.properties,
          classpath:user.properties"
ignore-unresolvable="true"/>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dao.properties</value>
                <value>classpath:services.properties</value>
                <value>classpath:user.properties</value>
            </list>
        </property> 
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

这篇关于在多个项目/模块中使用多个属性文件(通过PropertyPlaceholderConfigurer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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