spring boot 在哪里配置默认的application.properties

Gre*_*ves 5 spring spring-boot

默认情况下,Spring Boot 会自动从 classpath:/application.properties

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

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

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*zel 3

有很多方法可以覆盖属性键,而无需禁用整个外部化配置功能;这实际上就是目标。

您可以在此处查看属性的考虑顺序。例如,您可以将该外部属性文件添加到config打包 JAR 旁边的文件夹中,甚至可以自己配置文件位置

现在,如果您确实想禁用所有这些(并且 Boot 团队强烈建议不要这样做),您可以注册自己的EnvironmentPostProcessor请参阅此处)并PropertySources从中删除MutablePropertySources,您可以使用 获取它configurableEnvironment. getPropertySources()

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

  1. 这实际上发生在应用程序初始化阶段的早期,在自动配置之前
  2. 这不是你应该做的事情,因为它会产生很多副作用