Introduction to system initializer

The principle of

  1. The name of the class ApplicationContextInitializer
  2. Describes the callback functions that are executed before the Spring container is refreshed
  3. Register properties with the SpringBoot container
  4. Inherit interface custom implementation

implementation

Methods a

1. Create FirstInitializer class implements ApplicationContextInitializer interface

@Order(1)
public class FirstInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
        Map<String, Object> map = new HashMap<>();
        map.put("key1"."value1");
        MapPropertySource mapPropertySource = new MapPropertySource("firstInitializer", map);
        environment.getPropertySources().addLast(mapPropertySource);
        System.out.println("run firstInitializer"); }}Copy the code

2. Create a meta-INF /spring.factories file under resource and configure the properties

org.springframework.context.ApplicationContextInitializer=com.example.demo.initializer.FirstInitializer
Copy the code

Method 2

  1. Create SecondInitializer class implements ApplicationContextInitializer interface
@Order(2)
public class SecondInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
        Map<String, Object> map = new HashMap<>();
        map.put("key2"."value2");
        MapPropertySource mapPropertySource = new MapPropertySource("secondInitializer", map);
        environment.getPropertySources().addLast(mapPropertySource);
        System.out.println("run secondInitializer"); }}Copy the code
  1. In the startup class, add the initialization example manually
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {      
        SpringApplication application = new  SpringApplication(DemoApplication.class);
        application.addInitializers(newSecondInitializer()); application.run(args); }}Copy the code

Methods three

  1. Create ThirdInitializer class implements ApplicationContextInitializer interface
@Order(3)
public class ThirdInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();
        Map<String, Object> map = new HashMap<>();
        map.put("key3"."value3");
        MapPropertySource mapPropertySource = new MapPropertySource("thirdInitializer", map);
        environment.getPropertySources().addLast(mapPropertySource);
        System.out.println("run thirdInitializer"); }}Copy the code
  1. Add the configuration to application.properties under Resource
context.initializer.classes=com.example.demo.initializer.ThirdInitializer
Copy the code

Tips: application. The properties in the configuration, is DelegatingApplicationContextInitializer implement initialization configuration method, read the order = 0, so the first perform the initialization

Pay attention to

  1. If all implementation ApplicationContextInitializer interface
  2. The smaller the Order value, the earlier the execution
  3. The one defined in application.properties takes precedence over the others

SpringFactoriesLoader introduction

The principle of

  1. A generic factory loading mechanism used within the framework
  2. Read files from specific locations of multiple JAR packages in the CLASspath and initialize classes
  3. The file content must be of KV type, i.e., properties type
  4. The key is the fully qualified name (abstract classes | interface), the value is achieved, multiple implementations use, segmentation

Framework initialization steps

SpringFactoriesLoader role

The SpringBoot framework reads specific files from the classpath JAR package to load the extended classes

LoadFactoies process

System initializer principle analysis

role

  1. Context refresh is called before the refresh method
  2. Used to encode and set some property variables commonly used in web environments
  3. You can sort through the Order interface

Call the point

Calling process

Realize the principle of

  1. The definition is found and registered by the SpringFactoriesLoader in the Spring. factories file
  2. Manually add SpringApplication after initialization
  3. Defined as environment variables was DelegatingApplicationContextInitializer found registration

The interview questions

What about the Spring Elements Loader?

The SpringBoot factory load class that SpringBoot uses to load extension points

How does the SpringFactoriesLoader load the factory class?

Read the specified files in the specified path, read them into property objects, iterate through the contents of the file in turn, assemble into class names and corresponding implementations, sort by order.

What does the system initializer do?

It’s actually a callback interface to the SpringBoot container to which we can define our properties.

When is the system initializer called?

Called in the prepareContext procedure during springBoot boot