本文介绍了加载特定于环境的属性以与PropertyPlaceholderConfigurer一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常常见的问题,但我没有就最佳方法找到任何形式的共识,所以我在这里提出这个问题。

This seems like a pretty common problem, but I haven't found any sort of consensus on the best method, so I'm posing the question here.

我正在使用Spring Batch和Spring开发一个命令行Java应用程序。我正在使用属性文件和PropertyPlaceholderConfigurer,但我不太确定处理多个环境(dev,test等)的属性文件的最佳方法。我的谷歌搜索只是编写加载属性的程序化方式(即,在Java代码本身),这对我正在做的事情不起作用。

I'm working on a command-line Java application using Spring Batch and Spring. I'm using a properties file along with a PropertyPlaceholderConfigurer, but I'm a little unsure of the best way of handling the properties files for multiple environments (dev, test, etc.). My Googling is only turning up programmatic ways of loading the properties (i.e., in the Java code itself), which doesn't work for what I'm doing.

一个我考虑过的方法只是将每个环境的属性文件放在服务器上,并通过命令行参数将文件的目录添加到类路径中,但是我在使用该方法加载文件时遇到了问题。

One approach I've considered is simply placing each environment's properties file on the server and adding the file's directory to the classpath via a command-line argument, but I've been having trouble loading the file using that method.

我正在考虑的另一种方法是在jar中包含所有属性文件,并使用系统属性或命令行参数在运行时填写属性文件的名称,如下所示:

The other method I'm considering is to just include all the properties files in the jar and use a system property or command line argument to fill in the name of the properties file at runtime, like this:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:job.properties.${env}</value>
        </list>
    </property>
</bean>

我倾向于后一种解决方案,但我也想看看是否有更好的方法我我忽略了。

I lean towards the latter solution, but I'm also looking to see if there's a better method I'm overlooking.

我还应该提到我必须在运行时而不是在构建中进行替换。我被限制使用的过程需要一个单独的构建,它将通过环境升级到生产,所以我无法使用替换ala Maven或Ant。

I should also mention that I have to make the substitution at runtime rather than in the build. The process I'm constrained to use requires a single build which will be promoted through the environments to production, so I'm unable to use substitution ala Maven or Ant.

推荐答案

我同意 - 它不应该是构建时配置,因为您希望将完全相同的有效负载部署到各种上下文。

I agree - it should not be a build time configuration as you want to deploy the exact same payload to the various contexts.

PropertyPlaceHolderConfigurer的Locations属性可以采用各种类型的资源。也可以是文件系统资源或网址?因此,您可以将配置文件的位置设置为本地服务器上的文件,然后无论何时运行,它都将以该服务器上的配置文件指定的模式运行。如果你有针对特定运行模式的特定服务器,这将工作正常。

The Locations property of PropertyPlaceHolderConfigurer can take various types of resources. Can also be a filesystem resouce or a url? Thus you could set the location of the config file to a file on the local server and then whenever it runs it would run in the mode specified by the config file on that server. If you have particular servers for particular modes of running this would work fine.

虽然看起来你想要在不同模式下运行相同的应用程序服务器。在这种情况下,我建议通过命令行参数传递配置文件的位置。将此值传递给PropertyPlaceHolderConfigurer会有点棘手但不可能。

Reading between the lines though it seems you want to run the same application in different modes on the same server. What I would suggest in this case is to pass the location of the config file via a command line parameter. It would be a little tricky to pass this value into the PropertyPlaceHolderConfigurer but would not be impossible.

这篇关于加载特定于环境的属性以与PropertyPlaceholderConfigurer一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:21