简体   繁体   English

Spring Boot 外部应用程序.properties

[英]Spring boot external application.properties

I am developing a spring boot application我正在开发一个弹簧启动应用程序

I want to override some properties in src/main/resources/application.properties with an external file (eg /etc/projectName/application.properties ).我想用一个外部文件(例如/etc/projectName/application.properties )覆盖src/main/resources/application.properties中的一些属性。

I tried several methods:我尝试了几种方法:

  1. @PropertySource("file:/etc/projectName/application.properties") as annotation at ApplicationConfig.java @PropertySource("file:/etc/projectName/application.properties")作为ApplicationConfig.java的注解

  2. spring.config.location=/etc/projectName/application.properties in my application.properties in resources spring.config.location=/etc/projectName/application.properties在我的 application.properties 在resources

I tested it with spring.port .我用spring.port对其进行了测试。 The first method only added properties but didn't override them.第一种方法只添加了属性,但没有覆盖它们。

I always use --spring.config.location= in the command line as specified in the documentation , and you can put various files in this, one with default values and another with the overridden ones.我总是在文档中指定的命令行中使用--spring.config.location= ,您可以在其中放置各种文件,一个带有默认值,另一个带有覆盖的文件。

Edit:编辑:
Alternatively, you could also use something like :或者,您也可以使用类似的东西:

@PropertySources({
    @PropertySource("classpath:default.properties")
    , @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true)
})

and specify a external.config in your application.properties.并在 application.properties 中指定external.config
This would provide a default path for overridding config, that is still overriddable itself by specifying a --external.config in the command line.这将为覆盖配置提供默认路径,通过在命令行中指定--external.config仍然可以覆盖自身。
I use this with ${external.config} being defined as a system env variable, but it should work with a application.properties variable too.我将它与${external.config}定义为系统环境变量一起使用,但它也应该与 application.properties 变量一起使用。

I had the same requirement like yours( war package instead of a fat jar) and I manged to externalize the configuration file: In my main class I made this configuration:我有和你一样的要求(war包而不是fat jar),我设法将配置文件外部化:在我的主类中,我做了这个配置:

@SpringBootApplication
@PropertySource("file:D:\\Projets\\XXXXX\\Config\\application.properties")
public class WyApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WyApplication.class, args);
    }

}   

Hope this will help you.希望这会帮助你。 Good luck.祝你好运。

Notes笔记

1) Tomcat allows you to define context parameters 2) Spring Boot will read the properties defined in Tomcat context parameters as if you are defining them as -Dsomething=some_value 1) Tomcat 允许您定义上下文参数 2) Spring Boot 将读取 Tomcat 上下文参数中定义的属性,就好像您将它们定义为 -Dsomething=some_value

Options选项

Option 1: Hence a possible way is for you to define spring.config.location at the Tomcat context paramter:选项 1:因此,一种可能的方法是让您在 Tomcat 上下文参数中定义spring.config.location

<Context>
    <Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>

Option 2: define a system property at the Tomcat's setenv.sh file选项 2:在 Tomcat 的 setenv.sh 文件中定义系统属性

Dspring.config.location=/path/to/application.properties

Option 3: define an environment variable: SPRING_CONFIG_LOCATION选项3:定义环境变量: SPRING_CONFIG_LOCATION

If you do not want to use any solution from above, you could do the following in the start of your main(String args[]) method (before the usage of SpringApplication.run(MyClass.class, args) :如果您不想使用上面的任何解决方案,您可以在main(String args[])方法的开头执行以下操作(在使用SpringApplication.run(MyClass.class, args)

//Specifies a custom location for application.properties file.
System.setProperty("spring.config.location", "target/config/application.properties");

Note: The following solution will replace the old application.properties entirely注意:以下解决方案将完全替换旧的 application.properties

You can place your application.properties in one of the following locations:您可以将 application.properties 放在以下位置之一:

  • Under /config sub-directory of the current directory当前目录的/config子目录下
  • The current directory当前目录
  • A classpath /config package一个类路径 /config 包
  • The classpath root类路径根

And then exclude the default one under resources directory, like:然后排除资源目录下的默认目录,如:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/application.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

Find more details in this link在此链接中查找更多详细信息

Based on @JR Utily response this "format" work for me when use email config and multiple properties (I tried to use a PropertySourcesPlaceholderConfigurer Bean but for any reason don't take the email config):根据@JR Utily 的响应,这种“格式”在使用电子邮件配置和多个属性时对我有用(我尝试使用PropertySourcesPlaceholderConfigurer Bean,但出于任何原因不接受电子邮件配置):

@SpringBootApplication
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource(value = "file:/path/to/config1.properties", ignoreResourceNotFound = true),
    @PropertySource(value = "file:/path/to/config2.properties", ignoreResourceNotFound = true),
    @PropertySource(value = "file:/path/to/config3.properties", ignoreResourceNotFound = true)
})
public class ExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }
}

Yes we can add application.properties file from external folder.是的,我们可以从外部文件夹添加 application.properties 文件。 The simplest way is最简单的方法是

@SpringBootApplication
public class ExternalAppPropertiesTest{

    public static void main(String[] args) {
        System.setProperties("spring.config.location","D:\\yourfoldername where application.properties added");
        SpringApplication.run(ExternalAppPropertiesTest.class, args);
    }
}

NB - In case of externally application.properties access please don't use @PropertySources annotation and file path.注意 - 如果是外部 application.properties 访问,请不要使用 @PropertySources 注释和文件路径。 It will not access your logging properties eg logging.file.name=yourlogfilename.log它不会访问您的日志记录属性,例如 logging.file.name=yourlogfilename.log

Here is one more suggestion, for code maintenance you can put your applcation.properties by renaming the file name in your src folder of spring boot application and maintain the changes which is done in external file.这里还有一个建议,对于代码维护,您可以通过重命名 Spring Boot 应用程序的 src 文件夹中的文件名来放置您的 applcation.properties,并维护在外部文件中完成的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM