简体   繁体   English

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

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

We are currently writing an application which is split into multiple projects/modules. 我们目前正在编写一个分为多个项目/模块的应用程序。 For example, let's take the following modules: 例如,让我们采用以下模块:

  • myApp-DAO 对myApp-DAO
  • myApp-jabber 对myApp-叽里咕噜

Each module has its own Spring context xml file. 每个模块都有自己的Spring上下文xml文件。 For the DAO module I have a PropertyPlaceholderConfigurer which reads a property file with the necessary db connection parameters. 对于DAO模块,我有一个PropertyPlaceholderConfigurer,它使用必要的db连接参数读取属性文件。 In the jabber module I also have a PropertyPlaceHolderConfigurer for the jabber connection properties. 在jabber模块中,我还有一个用于jabber连接属性的PropertyPlaceHolderConfigurer。

Now comes the main application which includes myApp-DAO and myApp-jabber. 现在主要的应用程序包括myApp-DAO和myApp-jabber。 It reads all the context files and starts one big Spring context. 它读取所有上下文文件并启动一个大的Spring上下文。 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. 不幸的是,似乎每个上下文只能有一个PropertyPlaceholderConfigurer,因此首先加载的模块能够读取它的连接参数。 The other one throws an exception with an error like "Could not resolve placeholder 'jabber.host'" 另一个抛出异常,出现“无法解析占位符'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). 现在我已经将PropertyPlaceHolderConfigurer移出了单独的上下文文件,并将它们合并到主应用程序的上下文中(使用单个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. 这很糟糕,因为现在每个使用dao模块的人都必须知道,他们在上下文中需要一个PropertyPlaceHolderConfigurer .. dao模块中的集成测试也会失败等等。

I'm curious to hear about solutions/ideas from the stackoverflow community.. 我很想知道来自stackoverflow社区的解决方案/想法。

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>

I know that this is an old question, but the ignore-unresolvable property 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. 以防万一其他人碰到这个。

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

The PropertiesPlaceholderConfigurer bean has an alternative property called "propertiesArray". PropertiesPlaceholderConfigurer bean有一个名为“propertiesArray”的替代属性。 Use this instead of the "properties" property, and configure it with an <array> of property references. 使用此属性而不是“properties”属性,并使用<array>属性引用对其进行配置。

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: 如果Spring上下文中存在多个元素,则应遵循一些最佳实践:

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 需要指定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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring多个PropertyPlaceholderConfigurer文件和数据库 - Spring Multiple PropertyPlaceholderConfigurer files and database 春天的多种特性。 PropertyPlaceholderConfigurer类中的属性 - multiple properties in spring. property in PropertyPlaceholderConfigurer class 为什么多个PropertyPlaceholderConfigurer无法正常工作? - Why multiple PropertyPlaceholderConfigurer is not working? PropertyPlaceholderConfigurer,用于读取属性文件和数据库 - PropertyPlaceholderConfigurer for reading property files and database 如何使用 maven 在多模块项目中部署父模块和选定模块 - How to deploy parent and selected modules in a multiple module projects using maven 在Spring中使用PropertyPlaceholderConfigurer创建具有不同值的多个类实例 - creating multiple instances of class with diffrent values using PropertyPlaceholderConfigurer in Spring 创建PropertyPlaceHolderConfigurer的多个实例-Spring - Creating multiple instances of PropertyPlaceHolderConfigurer - Spring Spring PropertyPlaceholderConfigurer和具有多个值的键 - Spring PropertyPlaceholderConfigurer and keys with multiple values 多个Spring PropertyPlaceholderConfigurer同时出现 - Multiple Spring PropertyPlaceholderConfigurer at the same time SonarQube:如何分析共享多个模块的项目? - SonarQube: How to analyze projects that share multiple modules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM