使用相对于配置文件的路径引用Spring属性文件 [英] Reference Spring properties file using path relative to config file

查看:442
本文介绍了使用相对于配置文件的路径引用Spring属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将属性从Spring配置文件中移动到单独的属性文件中。这包含在配置文件中

I am moving properties from inside my Spring config file to a separate properties file. This is included in the config file with

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" value="file:properties/${CONFIG_MODE}/service.properties" />
</bean>

目前,属性文件的位置是相对于当前工作目录<服务器进程的/ em>。

As it stands, the location of the properties file is relative to the current working directory of the server process.

这就要求必须从特定的工作目录启动进程,更糟糕的是允许(确实是远程的)可能性它可以获取一个完全不同的属性文件 - 例如,如果它是在工作目录设置为较旧版本的服务的情况下启动的。

This creates the requirement that the process must be started from a specific working directory, and even worse allows for the (admittedly remote) possibility that it could pick up an entirely different properties file - for example if it was started with the working directory set to an older version of the service.

我想要使用相对于包含配置文件的目录的路径引用属性文件

I'd like to reference the properties file using a path that is relative to the directory containing the config file.

查看 FileSystemResource ,似乎 createRelative 可能就是我所需要的,但我无法弄清楚如何在配置文件中使用它。

Looking at FileSystemResource, it seems createRelative might be what I need, but I can't figure out how to use it in the config file.

谢谢,

史蒂夫

推荐答案

我不喜欢知道一种方法。

I don't know of a way to do that.

然而,你可以做的是从类路径加载属性文件:

What you can do, however, is load the properties file from the classpath:

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" value="classpath:path/to/service.properties" />
</bean>

属性文件的类路径位置是一个更加可预测的情况,并且它可以工作很长时间因为您的类路径设置正确。

The classpath location of your properties file is a far more predictable situation, and it'll work as long as your classpath is set up properly.

这篇关于使用相对于配置文件的路径引用Spring属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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