首页 文章

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

提问于
浏览
11

SpringApplication将从以下位置的application.properties文件加载属性,并将它们添加到Spring Environment: - 当前目录的A / config子目录 .

  • 当前目录
  • classpath / config包
  • 类路径根
    列表按优先级排序(在列表中较高位置定义的属性将覆盖在较低位置中定义的属性) .

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 outside 类路径或tomcat容器添加其他位置,如 d:\application.properties

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

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

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

4 回答

  • 0

    对我来说,最简单的方法是在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>
    
  • 1

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

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

    export spring_config_location=/usr/local/tomcat/conf/
    

    将属性文件放在该文件夹中 . 如果您有多个应用程序,则可以将每个应用程序的属性文件的名称设置为唯一 . 对于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);
        }
    }
    

    这将在使用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");
        }
    
    }
    

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

  • 8

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

    Tomcat classpath

    然后,在目录中放置您想要外部化的资源,一切运行正常 . 要在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>
    

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

    enter image description here

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

  • 8

    我最后添加了以下属性来外部化,例如安全属性:

    spring.config.additional-location=/etc/tomcat/<appname>/application-production.properties

相关问题