[英]SPRING MVC - Use an external file as @PropertySource
I was wondering how to use an external file to load some properties for my app in different contexts..我想知道如何使用外部文件在不同的上下文中为我的应用程序加载一些属性..
I know I can pass a file in PropertySource
annotation.我知道我可以在PropertySource
注释中传递一个文件。 The problem is the path of the file.问题是文件的路径。
I mean, it looks like it want a full path to the file, but different environments have different paths for Tomcat
directory.我的意思是,看起来它想要文件的完整路径,但是不同的环境对Tomcat
目录有不同的路径。
My question is: Where is the correct directory to store this files (it must be outside to the app dir otherwise on every deploy it is deleted) and how I can link it in my application.我的问题是:存储这些文件的正确目录在哪里(它必须在应用程序目录之外,否则在每次部署时都会被删除)以及我如何将它链接到我的应用程序中。
For instance, now i created a /config
directory inside the /webapps
directory of tomcat.例如,现在我在 tomcat 的/webapps
目录中创建了一个/config
目录。
From my Mac i can load the file in this way:从我的 Mac 我可以通过这种方式加载文件:
@PropertySource("file:/Library/Tomcat/webapps/config/db.properties" )
but my production application is on a Linux server and obviously the path is different...但我的生产应用程序在 Linux 服务器上,显然路径不同......
Any idea or, better, a best practice ti manage it?有什么想法,或者更好的是管理它的最佳实践?
Thanks谢谢
I have a similar problem to solve some time back, what we did was that we provided the external directory path to properties file at time of server boot up , as follows:前段时间我有一个类似的问题要解决,我们所做的是在服务器启动时提供属性文件的外部目录路径,如下所示:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
@Configuration
@PropertySources({@PropertySource("${ext.properties.dir:classpath:}/db.properties") })
public class ApplicationConfig {
}
For XML based configuration:对于基于 XML 的配置:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/db.properties" order="-1"
ignore-unresolvable="true" ignore-resource-not-found="true" />
And supplied ext.properties.dir
value from application-server/jvm
as per environment:并根据环境从application-server/jvm
提供ext.properties.dir
值:
For instance, for DEV:例如,对于 DEV:
-Dext.properties.dir=file:/dev/Library/Tomcat/webapps/config/
and for PROD:对于生产:
-Dext.properties.dir=file:/prod/Library/Tomcat/webapps/config/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.