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

查看:15
本文介绍了在多个项目/模块中使用多个属性文件(通过 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,它使用必要的数据库连接参数读取属性文件.在 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天全站免登陆