Use EnvironmentPostProcessor to customize startup configuration

Introduction

SpringBoot supports dynamic reading of files, leaving the extended interface org.springframework.boot.env.EnvironmentPostProcessor. This interface is under the spring package. Use this for centralized management of configuration files, without the need to configure configuration files for each project. This method is also an extension left by the springboot framework (you can extend it yourself)

Basic use

Create a configuration file under any path

Insert picture description here

Insert picture description here

Define MyEnvironmentPostProcessor to implement the EnvironmentPostProcessor interface

@Component
public class MyEnvironmentPostProcessor1 implements EnvironmentPostProcessor {
    
    
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    
    
        try {
    
    
            System.out.println("MyEnvironmentPostProcessor1");
            InputStream inputStream = new FileInputStream("D:\\test1.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("test", properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

Define a META-INF folder on the classpath and then create a spring.factories file under it and specify it in it:

Insert picture description here

test

@SpringBootApplication
public class DemoApplication {
    
    

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

    @Value("${jdbc.root.user}")
    String userName;

    @PostConstruct
    public void post() {
    
    
        System.out.println("--------------------------------");
        System.out.println(userName);
    }
}

Insert picture description here

The official website of the EnvironmentPostProcessor interface explains
that the application environment that allows the context of the custom application is refreshed before the context of the application is better than that of the application. That is, some system configuration can be set before the spring context is built
. The implementation class of EnvironmentPostProcessor must be in the META-INF/spring.factories file. Register and register the full class name.
The EnvironmentPostProcessor processor detects the Org.springframework.core.Ordered annotation, so that the corresponding instances will be called in the order of the @Order annotation.

Test the Order annotation

Define two configuration classes
Insert picture description here
Insert picture description here
Insert picture description here

Set the @Order() annotation for the same configuration file and the same key value in the same namespace. The result is that the smaller the value of @Order() is, the smaller the value of @Order() is executed later, and the most important thing is that it is executed later. Will overwrite the previous value

The overwriting process is as follows: the following name is a key of the current configuration file, which is equivalent to a namespace
Insert picture description here

Existing configuration files will be removed when addLast is executed, and PropertySource also rewrites equals and hashCode with name as the element for repeated judgments

Insert picture description here

Seeing the above, we can sum up two conclusions:
1: After the configuration properties of two names with the same name are executed, the previous ones will be overwritten. Note that they are removed and then added. If the content of the latter configuration file is different from the previous one , The previous configuration file will not be loaded even if there are other properties
2: Using this processing flow we can manipulate some configurations loaded by the IOC, such as operating the application.properties file

Override application.properties

Insert picture description here

Insert picture description here

Insert picture description here

Through the above code, you can see that Spring has set the corresponding name for the default configuration file. As long as we specify this name, we can overwrite his attribute value and complete the custom function implementation (the default configuration file is loaded before the custom configuration)

Application scenario
1: You can load files that are not in the Classpath path in this way.
2: You can overwrite the default configuration provided by the system.
3: When we introduce some third-party frameworks, we can replace the matching configuration files in this way

Guess you like

Origin blog.csdn.net/Octopus21/article/details/111083630