java - 如何在 Spring 的 Tomcat 网络服务器中外部化 application.properties?

标签 java spring tomcat spring-boot

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).

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

问题:在 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/

相关文章:

java - 我的 postman 中出现此错误(POST 和 DELETE 出现此错误),希望有人可以解决此问题

Tomcat 停止线程以避免潜在的内存泄漏

apache - 使用 IP 时连接被拒绝,但使用 localhost 时不连接

Java错误 "Exception in thread "主要“java.lang.NullPointerException”

java - android - 启动画面上的进度条

java - Spring MVC validator : validate using current session object

java - hibernate : classpath resource cannot be opened because it does not exist

java - 在 spring 项目上运行 tomcat 服务器时遇到问题

java - JSP搜索栏功能

java - itext pdf在setHeaderRows时重复行