【问题标题】:Where does spring boot configure default application.propertiesspring boot在哪里配置默认application.properties
【发布时间】:2017-01-29 05:57:26
【问题描述】:

默认情况下Spring Boot会自动从classpath:/application.properties加载属性

我想知道这个自动配置源代码在哪里。 我想从我的应用程序中排除。 即:@EnableAutoConfiguration(exclude=XXXXAutoconfiguration.class)

原因是: 因为我无法使用 @PropertySource 覆盖默认的 application.properties 的外部属性

@SpringBootApplication
@ComponentScan(basePackages = {"com.test.green.ws"})
@PropertySource(value = {"classpath:/application.properties", "file:/opt/green-ws/application.properties"})
public class GreenWSApplication {

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

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    有很多方法可以在不禁用整个外部化配置功能的情况下覆盖属性键;这就是我们的目标。

    你可以看到here the order the properties are considered in。例如,您可以将该外部属性文件添加到打包 JAR 旁边的 config 文件夹中,甚至可以添加到 configure the file location yourself 中。

    现在,如果您真的想要禁用所有这些(并且引导团队强烈建议不要这样做),您可以注册自己的 EnvironmentPostProcessor (see here) 并删除 @987654326 @ 来自MutablePropertySources,您可以使用configurableEnvironment. getPropertySources() 获取。

    没有更简单的方法可以做到这一点,因为:

    1. 这在应用程序初始化阶段非常早,在自动配置之前
    2. 这不是你应该做的,因为它会有很多副作用

    【讨论】:

    • 是的,我可以通过使用程序 agurments 和配置文件夹来覆盖配置。但我仍然想知道如何禁用 spring boot 属性配置。有没有办法做到这一点?
    • 更新了我的答案;如果您遇到 Boot 错误,请在错误跟踪器上提出问题时提及所有不推荐的自定义设置。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2020-02-13
    • 2023-04-10
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    相关资源
    最近更新 更多