简体   繁体   English

在spring中加载外部属性文件?

[英]Loading external properties file in spring?

I have below spring configuration. 我有弹簧配置以下。 I am using spring 3 and windows OS. 我使用的是Spring 3和Windows OS。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <value>file:/extprops/conf/usercred.properties</value>
    </list>
  </property>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

In above config, am trying to load external properties file file:/extprops/conf/usercred.properties and it is working. 在上面的配置中,我试图加载外部属性文件file:/extprops/conf/usercred.properties并且它正在工作。 Here I did not specify the drive location in which the properties file resides. 这里我没有指定属性文件所在的驱动器位置。 Do i need to specify the drive info in path like C or D drive ? 我是否需要在路径中指定驱动器信息,如CD驱动器? Do i need to keep App server and properties file in the same drive ? 我是否需要将App服务器和属性文件保存在同一个驱动器中?

If you don't specify drive, it uses drive from current work directory which is that you are using during the launch of a process. 如果未指定驱动器,则它将使用当前工作目录中的驱动器,该驱动器是在启动进程期间使用的驱动器。 So, it works, but unreliable - if you run the process eg by link from different drive, then it may not work. 因此,它可行,但不可靠 - 如果您运行该过程,例如通过不同驱动器的链接,那么它可能无法正常工作。 So, specify the drive too, like this: 所以,也指定驱动器,如下所示:

file:///C:/extprops/conf/usercred.properties

More info: http://en.wikipedia.org/wiki/File_url 更多信息: http//en.wikipedia.org/wiki/File_url

Also, a better/standard approach will be to create an environment variable in the system whose value will contain the path to the propery file. 此外,更好/标准的方法是在系统中创建一个环境变量,其值将包含propery文件的路径。

Then in the configuration file, you can write as : 然后在配置文件中,您可以写为:

file:${your.prop.folder}/myproperties.properties.

This Saves efforts when there's a change in your property file location. 这可以在您的属性文件位置发生更改时节省工作量。

我认为属性文件应该在类路径上,这是它的正确位置...因为它是独立于路径的方式所以如果操作系统是Windows或Linux,它不会有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM