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

查看:19
本文介绍了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.

以前的问题已经提到了这一点,并且有一篇很好的博客文章这里 描述了如何在每个上下文中使用 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>

此特定示例将收集类路径上的所有 default.properties、overrides.properties 和 APP_HOME 中的属性文件.现在您可以从 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天全站免登陆