如何在Spring的Tomcat Web服务器中外部化application.properties? [英] How to externalize application.properties in Tomcat webserver for Spring?

查看:447
本文介绍了如何在Spring的Tomcat Web服务器中外部化application.properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-应用程序属性文件

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

Question: when running a war file on a tomcat server: how can I add an additional location for the application.properties outside the classpath or the tomcat container, like d:\application.properties?

自定义位置应相对于上述位置具有最高优先级.

The custom location should get highest precedence regarding the locations above.

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

Problem is: I could of course add a /config folder inside my exploded war in the tomcat webapps folder, but then I'd lose any custom configuration if the webapps folder is cleaned and war is redeployed.

因此,我想在外面添加其他位置.

Thus I'd like to add an additional location outside.

推荐答案

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

You can set a spring_config_location environment variable pointing to the folder that contains your application.properties file.

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

In the case of Tomcat you can do this by adding the following line to your <TOMCAT_HOME>/bin/setenv.sh file (create the file if missing):

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

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

Place the properties file in that folder. In case you have multiple apps you can set the name of the properties file of each app to be unique. For a Spring Boot App I have done it like this:

@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的配置:

This will pick the new name when run with BOOT. To have the name configured when deployed on Tomcat too, overwrite configure of SpringBootServletInitializer like so:

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

Then name your properties file like: my-app.properties. Instead of the default name Spring will look for that. You can put all your apps properties files in the specified folder, /usr/local/tomcat/conf/ in our sample. Your external properties will get precedence. See here for priorities: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

更新

Spring Boot 2 起,spring_config_location的行为已更改(来自

Since Spring Boot 2 the behavior of spring_config_location has changed (from the migration guide):

它以前在默认位置列表中添加了一个位置,现在 替换默认位置.如果您依靠的是 以前处理过,现在应该使用 改为spring.config.additional-location.

it previously added a location to the list of default ones, now it replaces the default locations. If you were relying on the way it was handled previously, you should now use spring.config.additional-location instead.

因此,根据您的用例,您应该考虑将哪些属性设置为环境变量.新版本应类似于setenv.sh中的spring_config_additional-location. 在

So based on your use case you should consider which of the properties to set as an environment variable. The new one should look like spring_config_additional-location in setenv.sh. Where files are looked up from is described in the reference documentation too.

这篇关于如何在Spring的Tomcat Web服务器中外部化application.properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆