Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

java - Multiple properties files in Spring 3.0

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.

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.

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

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

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?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...