Spring引导外部application.properties

时间:2015-01-04 22:03:30

标签: java spring-boot

我正在开发一个spring boot应用程序

我想使用外部文件(例如src/main/resources/application.properties)覆盖/etc/projectName/application.properties中的某些属性。

我尝试了几种方法:

  1. @PropertySource("file:/etc/projectName/application.properties")作为ApplicationConfig.java

  2. 的注释 我在spring.config.location=/etc/projectName/application.properties

    中的application.properties中的
  3. resources

    我用spring.port测试了它。第一种方法只添加了属性,但没有覆盖它们。

6 个答案:

答案 0 :(得分:18)

我总是在documentation中指定的命令行中使用--spring.config.location=,你可以在其中放入各种文件,一个是默认值,另一个是被覆盖的文件。

编辑:
或者,您也可以使用以下内容:

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

并在application.properties中指定external.config 这将提供覆盖配置的默认路径,通过在命令行中指定--external.config,它仍然可以覆盖。 我使用它,并将${external.config}定义为系统env变量,但它也应该与application.properties变量一起使用。

答案 1 :(得分:0)

注释

1)Tomcat允许您定义上下文参数 2)Spring Boot将读取Tomcat上下文参数中定义的属性,就像您将它们定义为-Dsomething = some_value

选项

选项1:因此,一种可能的方法是在Tomcat上下文参数中定义 spring.config.location

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

选项2:在Tomcat的setenv.sh文件中定义系统属性

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

选项3:定义环境变量: SPRING_CONFIG_LOCATION

答案 2 :(得分:0)

如果您不想使用上面的任何解决方案,则可以在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");

答案 3 :(得分:0)

我有与您相同的要求(使用war包而不是胖罐子),并且设法将配置文件外部化: 在我的主课堂上,我进行了以下配置:

@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);
    }

}   

希望这会对您有所帮助。 祝你好运。

答案 4 :(得分:0)

注意:以下解决方案将完全取代旧的application.properties

您可以将application.properties放在以下位置之一:

  • 当前目录的/ config子目录下
  • 当前目录
  • 类路径/ config包
  • classpath根

然后排除资源目录下的默认目录,例如:

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

在此link

中查找更多详细信息

答案 5 :(得分:0)

根据 @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);
    }
}