2013-07-03 58 views
1

我的问题是这个问题的一个完全相同的副本: Spring application context external properties?如何注入的外部属性在Spring上下文文件

这是我如何注入道具之前文件:

<util:properties id="smrProps" location="classpath:/spring/SomeProps.properties" /> 

但对于生活我不知道在location中使用什么时,我想注入一个属性文件,它将与可运行的jar文件位于同一个目录中。我知道classpath指向的地方,我的道具只有一层,所以我甚至试着假设classpath:/../SomeProps.properties会在父文件夹中查找,但没有运气。

对于离如果罐子是:C:\temp\some.jar和属性文件是C:\temp\SomeProps.properties

如果some.jar是在温度,然后SomeProps.properties也将在温度。当然,我不能在位置使用C:\ temp \ SomeProps.properties

有人能指导我如何使用这个道具文件吗?

+0

如果您在使用Spring启动时,您可以校验H TTP://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – shobull

回答

0

我不认为有可能使用相对路径访问.jar以外的东西util:properties

但是,如果可以的话,您应该可以通过使用系统属性来完成。

此外,你应该使用file:而不是classpath:。事情是这样的:

<util:properties id="smrProps" location="file:{my.path}/SomeProps.properties" /> 

然后用像这样执行的.jar:

java -Dmy.path=/YourFolderPath -jar application.jar 

的其他解决办法是延长PropertyPlaceholderConfigurer然后拿到.jar的路径与Java。看看这个问题:Loading Properties with Spring (via System Properties)

0

另一个解决办法是找出运行罐子

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()); 

用它找到你的道具文件中的父文件夹,并用它注入方面SPEL

的路径

下面是它的外观(您可能希望把它打扫干净了一点,但我能模仿你的情况,并证实了这一解决方案):

<util:properties id="smrProps" location="'file:' + new java.io.File(T(com.yourpackage.YourClass).getProtectionDomain().getCodeSource().getLocation().getPath()).getParent() + '/'+ settings.prop'" /> 
相关问题