Spring 3.0中的多个属性文件 [英] Multiple properties files in Spring 3.0

查看:86
本文介绍了Spring 3.0中的多个属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含多个单独模块的项目,每个模块都有自己的应用程序上下文属性文件。我希望能够加载所有这些属性,以便它们可以用于Spring的占位符解析。

I am working on a project with several separate modules, each with their own application context properties files. I want to be able to load all these properties so that they can be used for placeholder resolution by Spring.

之前的问题已经提到了这个并且有一篇很好的博客文章< a href =http://tarlogonjava.blogspot.com/2009/02/tips-regarding-springs.html\"rel =noreferrer>这里描述了如何在每个上下文中使用PropertyPlaceholderConfigurer,订购它们按优先级将ignoreUnresolveablePlaceholders设置为true,这样这些属性文件就可以相互交叉引用而不会爆炸。

Previous questions have mentioned this and there is a good blog post here that describes how to use a PropertyPlaceholderConfigurer in each context, order them by priority and set ignoreUnresolveablePlaceholders to true so that these properties files can cross-reference each other without blowing up.

然而,这并不能解决我的问题,因为我也想要能够使用我正在加载的属性来进行一些自定义占位符解析(从我正在解析的一些yaml文件)。这需要使用PropertyPlaceholderHelper,它需要将Properties对象作为参数。

However, this doesn't solve my problem as I also want to be able to use the properties I am loading for some custom placeholder resolution (from some yaml files I am parsing). This requires using a PropertyPlaceholderHelper, which requires the Properties object as an argument.

据我所知,潜在的解决方案是:

As far as I can tell, potential solutions are:

1)将所有属性文件合并为一个属性bean。然后可以使用它来创建PropertyPlaceholderConfigurer(用于Spring的内部占位符解析)并与PropertyPlaceholderHelper一起使用(用于我自己的占位符解析)

1) Merge all the properties file into one properties bean. This can then be used to create a PropertyPlaceholderConfigurer (for Spring's internal placeholder resolution) and used with a PropertyPlaceholderHelper (for my own placeholder resolution)

2)以某种方式将PropertyPlaceholderHelper配置为如果我继续并遵循该博客帖子的建议,请使用PropertyPlaceholderConfigurers持有的属性集及其分层排列。

2) Somehow configure a PropertyPlaceholderHelper to use the set of properties, and their hierarchical arrangement, held by the PropertyPlaceholderConfigurers if I go ahead and follow the advice of that blog post.

不幸的是我无法解决怎么做其中任何一个。任何帮助将不胜感激!

Unfortunately I can't work out how to do either of these. Any help would be greatly appreciated!

PS看起来Spring 3.1将是一个很大的帮助...不幸的是我们还没有准备好移动它所以我仍然需要一个解决方案让我度过难关!

PS It looks like Spring 3.1 will be a big help here...unfortunately we aren't ready to move to it yet so I still need a solution to tide me over!

****编辑****

**** EDIT ****

谢谢你答案到目前为止。它们是很好的答案,但不幸的是我不会帮助我(因为我们之前没有提到这一点而道歉)我们目前正在将我们项目的核心模块与非核心模块分开。这意味着核心模块及其应用程序上下文无法对属性文件的名称进行硬编码。令人沮丧的是,Spring的类路径扫描似乎被打破了,所以classpath *:*。properties类型的通配符仅在构建单个模块时起作用,而不是顶层项目(我相信这是一个已知问题)。

Thanks for the answers so far. They are good answers but unfortunately won't help me because (and apologies for not mentioning this earlier) we are currently in the process of separating out the core modules of our project from the non-core modules. This means that the core modules, and their application context, cannot hard-code the names of the properties files. Frustratingly, Spring's classpath scanning appears to be broken, so wildcards of the type "classpath*:*.properties" only work when building the individual modules, not the top level project (I believe this is a known issue).

问题是如何将非核心模块中定义的属性文件合并到核心模块中定义的现有属性文件中。目前我正在使用BeanPostProcessor - 我只是想知道是否有更简单/更优雅的方法来做到这一点?

The question is then how to merge properties files defined in non-core modules into existing properties files defined in core modules. At the moment I am going ahead with a BeanPostProcessor - I am just wondering if there is a simpler/more elegant way to do this?

谢谢

推荐答案

您可以非常轻松地将多个属性文件收集到一个bean中:

You can collect multiple property files into one bean quite easily:

<bean id="allProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="singleton" value="true"/>
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="locations">
    <list>
      <value>classpath*:default.properties</value>
      <value>classpath*:overrides.properties</value>
      <value>file:${APP_HOME}/**/*.properties</value>
    </list>
  </property>
</bean>

此特定示例将收集APP_HOME中类路径和属性文件上的所有default.properties,overrides.properties 。现在,您可以从ProperyPlaceholderConfigurer或您的自定义后处理器中引用此bean。

This particular example will gather all default.properties, overrides.properties on classpath and property files in your APP_HOME. Now you can refer to this bean from ProperyPlaceholderConfigurer or your custom postprocessor.

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

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