Java 在多个项目/模块中使用多个属性文件(通过 PropertyPlaceholderConfigurer)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3403773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:59:04  来源:igfitidea点击:

Using multiple property files (via PropertyPlaceholderConfigurer) in multiple projects/modules

javaspringproperties

提问by black666

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
  • myApp-DAO
  • myApp-jabber

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.

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

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'"

现在是主要应用程序,其中包括 myApp-DAO 和 myApp-jabber。它读取所有上下文文件并启动一个大的 Spring 上下文。不幸的是,似乎每个上下文只能有一个 PropertyPlaceholderConfigurer,因此无论哪个模块首先加载都能够读取它的连接参数。另一个抛出异常并显示错误,例如“无法解析占位符 '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.

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

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.

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

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

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

采纳答案by Tim Hennekey

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"/>

or

或者

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

回答by Stephen C

The PropertiesPlaceholderConfigurerbean has an alternative property called "propertiesArray". Use this instead of the "properties" property, and configure it with an <array>of property references.

PropertiesPlaceholderConfigurerbean有另一种性质叫“propertiesArray”。使用它而不是“属性”属性,并使用<array>属性引用对其进行配置。

回答by earldouglas

You can have multiple <context:property-placeholder />elements instead of explicitly declaring multiple PropertiesPlaceholderConfigurer beans.

您可以拥有多个<context:property-placeholder />元素,而不是显式声明多个 PropertiesPlaceholderConfigurer bean。

回答by Raul Rene

I know that this is an old question, but the ignore-unresolvableproperty was not working for me and I didn't know why.

我知道这是一个老问题,但该ignore-unresolvable物业对我不起作用,我不知道为什么。

The problem was that I needed an external resource (something like location="file:${CATALINA_HOME}/conf/db-override.properties") and the ignore-unresolvable="true"does not do the job in this case.

问题是我需要一个外部资源(比如location="file:${CATALINA_HOME}/conf/db-override.properties"),而ignore-unresolvable="true"在这种情况下它不起作用。

What one needs to do for ignoring a missing external resource is:

忽略缺少的外部资源需要做的是:

ignore-resource-not-found="true"

Just in case anyone else bumps into this.

以防万一其他人碰到这个。

回答by onurbaysan

I tried the solution below, it works on my machine.

我尝试了下面的解决方案,它适用于我的机器。

<context:property-placeholder location="classpath*:connection.properties" ignore-unresolvable="true" order="1" />

<context:property-placeholder location="classpath*:general.properties" order="2"/>

In case multiple elements are present in the Spring context, there are a few best practices that should be followed:

the order attribute needs to be specified to fix the order in which these are processed by Spring all property placeholders minus the last one (highest order) should have ignore-unresolvable=”true”to allow the resolution mechanism to pass to others in the context without throwing an exception

如果 Spring 上下文中存在多个元素,则应遵循一些最佳实践:

需要指定 order 属性来修复 Spring 处理这些的顺序所有属性占位符减去最后一个(最高顺序)应该ignore-unresolvable=”true”允许解析机制传递给上下文中的其他人而不会抛出异常

source: http://www.baeldung.com/2012/02/06/properties-with-spring/

来源:http: //www.baeldung.com/2012/02/06/properties-with-spring/