如何在Spring的Tomcat webserver中外化application.properties?

mem*_*und 14 java spring tomcat spring-boot

SpringApplication将从以下位置的application.properties文件加载属性,并将它们添加到Spring环境中:

- A /config subdirectory of the current directory.
- The current directory
- A classpath /config package
- The classpath root
Run Code Online (Sandbox Code Playgroud)

列表按优先级排序(在列表中较高位置定义的属性将覆盖在较低位置中定义的属性).

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

问题:在服务器war上运行文件时tomcat:如何为类路径或tomcat容器application.properties 外部添加其他位置d:\application.properties

自定义位置应优先于上述位置.

问题是:我当然可以/config在tomcat webapps文件夹中的爆炸战争中添加一个文件夹,但是如果清理了webapps文件夹并重新部署了war,我将失去任何自定义配置.

因此,我想在外面添加一个额外的位置.

Vla*_*tev 14

您可以设置spring_config_location指向包含application.properties文件的文件夹的环境变量.

对于Tomcat,您可以通过在<TOMCAT_HOME>/bin/setenv.sh文件中添加以下行来完成此操作(如果缺少则创建文件):

export spring_config_location=/usr/local/tomcat/conf/
Run Code Online (Sandbox Code Playgroud)

将属性文件放在该文件夹中.如果您有多个应用程序,则可以将每个应用程序的属性文件的名称设置为唯一.对于Spring Boot App,我这样做了:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "my-app");
        SpringApplication.run(MyApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将在使用BOOT运行时选择新名称.要在Tomcat上部署时配置名称,请覆盖如下配置SpringBootServletInitializer:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class).properties("spring.config.name: my-app");
    }

}
Run Code Online (Sandbox Code Playgroud)

然后命名你的属性文件,如:my-app.properties.而不是默认名称Spring会寻找它.您可以/usr/local/tomcat/conf/在我们的示例中将所有应用程序属性文件放在指定的文件夹中.您的外部属性将优先.请参阅此处了解优先级:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html


Grz*_* B. 11

对我来说,最简单的方法是在Tomcat的config文件夹中放置一个上下文文件.例如,如果您的应用程序在根路径下运行(例如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>
Run Code Online (Sandbox Code Playgroud)


jlu*_*etu 1

我不得不这样做几次,我发现最好的方法是将外部目录配置为容器中的类路径资源:

Tomcat 类路径

然后,在目录中放置您想要外部化的资源,一切都会正常运行。要在 spring 中加载资源,可以这样做:

<beans:bean id="externalProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <beans:property name="location" value="classpath:[my-application-name]/applicationProperties.properties" />
            <beans:property name="placeholderPrefix" value="!applicationProperties{" />
            <beans:property name="placeholderSuffix" value="}" />
        </beans:bean>
Run Code Online (Sandbox Code Playgroud)

可以看到,正如您所说,您可能希望在每个tomcat中部署多个应用程序,您可以简单地在classpath中设置的文件夹中创建一个目录结构,为application.properties每个war应用程序维护不同的目录结构

在此输入图像描述

如果您想在 Spring 配置中动态维护应用程序名称部分,您可以通过多种方式来实现,在 Maven 中的打包阶段甚至使用应用程序上下文路径