Java spring-boot 应用程序的外部配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29106579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:22:15  来源:igfitidea点击:

External configuration for spring-boot application

javaspring-boottomcat

提问by Aram Aslanyan

I have a spring-boot application which I want to run with external configuration file. When I run it as jar (with embedded servlet container), everything is fine. But I want to run it under external servlet container (Tomcat) and here i have problem with external configuration. I have tried a @PropertySource, but in this case application gets only properties absent in war file configuration: external configuration doesn't override internal configuration. So the question: how can I configure external configuration which will override internal configuration?

我有一个 spring-boot 应用程序,我想用外部配置文件运行它。当我将它作为 jar(带有嵌入式 servlet 容器)运行时,一切都很好。但是我想在外部 servlet 容器(Tomcat)下运行它,在这里我遇到了外部配置问题。我尝试了@PropertySource,但在这种情况下,应用程序仅获取战争文件配置中不存在的属性:外部配置不会覆盖内部配置。所以问题是:如何配置将覆盖内部配置的外部配置?

采纳答案by ci_

You're probably using external configuration in the form of application.propertiesin the current directory when you're running your application as a jar. However, "current directory" isn't very useful when deploying as a war in an external tomcat. Even if you find out what the current directory is, it's most likely the same location for all applications running in that tomcat, so when you're running more than one application, that's not going to work very well.

application.properties当您将应用程序作为 jar 运行时,您可能正在以当前目录中的形式使用外部配置。但是,在外部 tomcat 中作为战争部署时,“当前目录”并不是很有用。即使您知道当前目录是什么,对于在该 tomcat 中运行的所有应用程序来说,它很可能位于相同的位置,因此当您运行多个应用程序时,这不会很好地工作。

What we do here is this declare two PropertySourceson our application:

我们在这里做的是PropertySources在我们的应用程序中声明两个:

@PropertySources({@PropertySource(value={"classpath:internal.properties"}), @PropertySource(value={"file:${application.properties}"})})

internal.propertiescontains "built in" default values for propeties. The second PropertySourceis a file containing external configuration. Note how the name of the file is itself a property.

internal.properties包含属性的“内置”默认值。第二个PropertySource是包含外部配置的文件。请注意文件的名称本身是一个属性。

We define this externally in the Contextelement of our application (in tomcat):

我们在Context应用程序的元素中(在 tomcat 中)在外部定义它:

<Context docBase="/path/to/your/war/your.war">
    <Parameter name="application.properties" value="/path/to/your/properties/application.properties"/>
</Context>

This allows you to have multiple applications running in tomcat, each application using it's own external properties file. You can even have multiple instances of the sameapplication running with different properties.

这允许您在 tomcat 中运行多个应用程序,每个应用程序使用它自己的外部属性文件。您甚至可以使用不同的属性运行同一应用程序的多个实例。

回答by Daniel Mora

To externalize the Spring Boot application.properties when deploying as a war file you can set spring.config.locationat the beginning when Spring Boot application is configured:

要在部署为 war 文件时外部化 Spring Boot application.properties,您可以spring.config.location在配置 Spring Boot 应用程序时在开始时进行设置:

public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
        return springApplicationBuilder
                .sources(Application.class)
                .properties(getProperties());
    }

    public static void main(String[] args) {

        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
                .sources(Application.class)
                .properties(getProperties())
                .run(args);
    }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
   }

For more details check this solution.

有关更多详细信息,请查看此解决方案

回答by Rafael Membrives

Spring Boot offer many waysto specify the location of your properties, it′s not needed to modify your sources.

Spring Boot 提供了多种方式来指定属性的位置,不需要修改源。

Yo can define the spring.config.locationvalue for example:

你可以定义spring.config.location值,例如:

  • In your tomcat/conf/Catalina/<host>context descriptors:

    <Context>
        <Parameter name="spring.config.location" value="/path/to/application.properties" />
    </Context>
    
  • As a JVM parameter in your tomcat setenv.shfile:

    -Dspring.config.location=/path/to/application.properties
    
  • As a SPRING_CONFIG_LOCATIONenvironment variable.

  • 在您的tomcat/conf/Catalina/<host>上下文描述符中:

    <Context>
        <Parameter name="spring.config.location" value="/path/to/application.properties" />
    </Context>
    
  • 作为 tomcatsetenv.sh文件中的 JVM 参数:

    -Dspring.config.location=/path/to/application.properties
    
  • 作为SPRING_CONFIG_LOCATION环境变量。

回答by mertaksu

You can add configuration files folder to set Classpathline catalina.bat, catalina.sh(which one if you want to use.) or you can add to setenv.bat/sh file. Your config files will be added to war classpath.

您可以添加配置文件文件夹以设置 Classpath行 catalina.bat、catalina.sh(如果要使用哪个。)或者您可以添加到 setenv.bat/sh 文件。您的配置文件将被添加到 war 类路径中。

For Example;

例如;

In Windows env.

在 Windows 环境中。

set CLASSPATH=D:\app\conf