Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does spring boot configure default application.properties

By default Spring Boot will automatically load properties from classpath:/application.properties

I want to know where is this auto configuration source code. I want to exclude from my app. IE: @EnableAutoConfiguration(exclude=XXXXAutoconfiguration.class)

The reason is: Because I cannot override the default application.properties by an external property using @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);
    }
}
like image 318
Greenleaves Avatar asked Oct 19 '22 01:10

Greenleaves


1 Answers

There are many ways to override property keys without disabling the whole externalized configuration feature; and that's actually the goal.

You can see here the order the properties are considered in. For example, you can add that external properties file in a config folder right next to the packaged JAR, or even configure the file location yourself.

Now if you really want to disable all of that (and the Boot team strongly suggests not to do that), you can register your own EnvironmentPostProcessor (see here) and remove PropertySources from MutablePropertySources, which you can fetch with configurableEnvironment. getPropertySources().

There's no easier way to do that because:

  1. this comes really early in the application init phase, before auto-configurations
  2. this is not something you should do, as it will have many side effects
like image 82
Brian Clozel Avatar answered Nov 15 '22 05:11

Brian Clozel