SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
- A /config subdirectory of the current directory. - The current directory - A classpath /config package - The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
问题:在 tomcat
服务器上运行 war
文件时:如何为 application.properties
添加其他位置 在类路径或 tomcat 容器之外,例如 d:\application.properties
?
自定义位置应优先于上述位置。
问题是:我当然可以在 tomcat webapps
文件夹中的爆炸 war 中添加一个 /config
文件夹,但是如果webapps 文件夹被清理,war 被重新部署。
因此我想在外面添加一个额外的位置。
最佳答案
对我来说,最简单的方法是在 Tomcat 的配置文件夹中放置一个上下文文件。例如,如果您的应用程序在根路径下运行(例如 http://your_domain.com/
),您需要创建一个文件 [path_to_your_tomcat]/conf/Catalina/localhost/ROOT。 xml
。如果您的应用程序在不同的路径中运行,例如 http://your_domain.com/example_path
该文件应命名为 [path_to_your_tomcat]/conf/Catalina/localhost/example_path.xml
。在此文件中,您可以指定外部 application.properties 文件的路径,该文件可以放在硬盘上的任何位置。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="spring.config.location" value="file:/path/to/your/application/properties/file/" type="java.lang.String"/>
</Context>
关于java - 如何在 Spring 的 Tomcat 网络服务器中外部化 application.properties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461786/