SpringBoot special study Part23: Principle of operation principle SpringBoot start automatically configures principle and listener listens order

SpringBoot There are four important event callback mechanism

  • ApplicationContextInitializer
  • SpringApplicationRunListener
  • ApplicationRunner
  • CommandLineRunner

Revolve around these four interfaces can understand the principle of operation principle SpringBoot start automatic configuration principle

When you start calling the run SpringApplication class () method

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

First, create SpringApplication objects:

SpringBoot program from start classes main()start as an entry method
to create SpringApplication objects passing in args parameter followed by run run()method

In this method, some of the attributes assigned to the default values and then call the initialize()method

Determine the type of the primary configuration classes in the process and save then determines whether the current application is a Web application
then goes into setInitializers () method from the class assignment to find the path /META-INF/spring.factoriesof looking for all configured ApplicationContextInitializer saved class
after also from the find all ApplicationListener saved under the path

From then find more configuration class has main()a method that is considered class is the main class configuration
thus run when SpringBoot applications allow incoming class allows multiple configurations incoming array format
so far SpringApplication the object is created

Two, run ():

Creates an empty ConfigurableApplicationContext
then the default class path /META-INF/spring.factoriesacquired in SpringApplicationRunListener
enter listener.starting after acquisition is completed () and traversing all the listener starting the start () method
with the args argument ApplicationArguments package
into the preparation method returns ConfigurableEnvironment environment objects
in preparation environmental method creates a ConfigurableEnvironment class and configure the environment
callback environment created after the completion of all SpringApplicationRunListener a environmentPrepared()way to represent a ready environment

After if not the Web environment is converted to a Web environment
and console print Spring icons
create ApplicationContext according to create the type of container
to create after reflection the IOC container and then call prepareContext()ready context the environment saved to the IOC container
and then call applyInitializer () Gets all Initializer before the callback all saved ApplicationContextInitializer the initialize()method
further pullback all SpringApplicationRunListener the contextPrepare()logo to be printed and parameters obtained before the method to register
after completion callback prepareContext run all SpringApplicationRunListener the contextLoaded()method

Call refreshContext () method to refresh the vessel if created for the embedded Tomcat Web application will create an add-in short, is to scan all of the components (Configuration class components to automatically configure)
then call afterRefresh () method which calls callRunners () method to get from the IOC container All ApplicationRunner (callback order is first), and CommandLineRunner (callback order is) callback
last all SpringApplicationRunListener callback running()method
to this application startup completion SpringBoot entire IOC returns to the container after the startup is completed to start


Third, the listener test

Test listener listens timing of the program is running in the
need to configure the META-INF / spring.factories categories:

  • ApplicationContextInitializer
  • SpringApplicationRunListener

IOC container need only be placed in classes:

  • ApplicationRunner
  • CommandLineRunner

1、ApplicationContextInitializer

Creating a class that implements ApplicationContextInitializer interfaces generics ConfigurableApplicationContext

public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("HelloApplicationContextInitializer ===> initialize : "+configurableApplicationContext);
    }
}

2、SpringApplicationRunListener

Creating a class that implements SpringApplicationRunListener Interface

public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {

    // 需要一个有参构造器
    public HelloSpringApplicationRunListener(SpringApplication application,String[] args) {
    }

    @Override
    public void starting() {
        // 开始监听容器 环境准备
        System.out.println("HelloSpringApplicationRunListener ==> starting");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        // 环境准备完毕
        System.out.println("HelloSpringApplicationRunListener ==> environmentPrepared : "+environment);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        // IOC容器准备完毕
        System.out.println("HelloSpringApplicationRunListener ==> contextPrepared : "+context);
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        // 容器环境加载完成
        System.out.println("HelloSpringApplicationRunListener ==> contextLoaded : "+context);
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        // 容器被refreshed 应用启动
        System.out.println("HelloSpringApplicationRunListener ==> started : "+context);
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        // run方法完成 开始运行
        System.out.println("HelloSpringApplicationRunListener ==> running");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        // 运行失败
        System.out.println("HelloSpringApplicationRunListener ==> failed : "+context);
    }
}

3、ApplicationRunner

Creating a class that implements ApplicationRunner Interface

@Component
public class HelloApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("HelloApplicationRunner ==> run : "+args);
    }
}

4、CommandLineRunner

Creating a class that implements CommandLineRunner Interface

@Component
public class HelloCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("HelloCommandLineRunner ==> run : "+Arrays.asList(args));
    }
}

To add to the container based only on the class to add annotations to @Component

To configure the META-INF class must create resources packet at the packet
which is used to create a profile configuration path spring.factories:
(\ represent the newline separated by commas plurality of paths)

# Initializers
org.springframework.context.ApplicationContextInitializer=\
net.zjitc.springboot.listener.HelloApplicationContextInitializer

# Listener
org.springframework.boot.SpringApplicationRunListener=\
net.zjitc.springboot.listener.HelloSpringApplicationRunListener

Effect - monitor order:
Here Insert Picture Description


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/105132123