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

时间:2017-01-04 10:39:31

标签: 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
     

列表按优先顺序排列(在位置中定义的属性)   列表中的较高值会覆盖较低位置中定义的值。

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

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

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

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

8 个答案:

答案 0 :(得分:11)

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

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

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

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

@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

<强>更新

由于 Spring Boot 2 spring_config_location的行为发生了变化(来自migration guide):

  

它以前在默认列表中添加了一个位置,现在它   替换默认位置。如果你依赖它的方式   以前处理过,你现在应该使用   相反,是spring.config.additional-location。

因此,根据您的用例,您应该考虑将哪些属性设置为环境变量。新的应该在spring_config_additional-location中看起来像setenv.sh。 查询文件的位置也在reference documentation中描述。

答案 1 :(得分:9)

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

答案 2 :(得分:1)

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

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中部署多个应用程序,您只需在类路径中设置的文件夹中创建目录结构,以便为每个tomcat维护不同的application.properties您的war申请

enter image description here

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

如果有人正在寻找linux解决方案,这对我们有用:
编辑tomcat startup.sh
添加:

 export spring_config_location=/<YOUR_PATH>/application.properties

示例:

 export spring_config_location=/app/conf/application.properties

答案 5 :(得分:0)

对于Ubuntu 18.04和Spring Boot 2上的tomcat 9,在$ CATALINA_HOME / bin /下创建setenv.sh文件:

#!/bin/bash

export spring_config_additional_location="/opt/tomcat/latest/conf/application.properties"

如果需要,请不要忘记设置文件权限

答案 6 :(得分:0)

在tomcat / bin中创建setenv.sh文件,在该文件中,您需要在文件中的以下行提供所有战争的默认属性,然后保存并重新启动tomcat。

export SPRING_PROFILES_ACTIVE=dev

答案 7 :(得分:0)

使用Tomcat 9.0.27

运行Spring Boot 2.2.1

我确实设置了setenv.sh并添加了行

export spring_config_location=/<PATH_TO_CONF_DIR>/application.properties

并使它工作。.

相关问题