SpringBoot启动过程

x33g5p2x  于2021-03-14 发布在 Spring  
字(4.0k)|赞(0)|评价(0)|浏览(143)

我们可以实现如下四个接口来观察SpringBoot的启动过程:

// 下面的2个需要在resources/META-INF/spring.factories文件中注入到IoC容器中
ApplicationContextInitializer
SpringApplicationRunListener

// 下面这两个需要标注@Component注入到IoC容器中
ApplicationRunner
CommandLineRunner

1. 实现的4个接口如下
/listener/MyApplicationRunner.java

package com.exa.process.listener;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

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

/listener/MyCommandLineRunner.java

package com.exa.process.listener;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner——>run()");
    }
}

/listener/MyApplicationContextInitializer.java

package com.exa.process.listener;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 监听ConfigurableApplicationContext的启动
 */
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("ApplicationContextInitializer——>initialize()");
    }
}

/listener/MySpringApplicationRunListener.java

package com.exa.process.listener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class MySpringApplicationRunListener implements SpringApplicationRunListener {

    public MySpringApplicationRunListener(SpringApplication application, String[] args) {

    }

    @Override
    public void starting() {
        System.out.println("SpringApplicationRunListener——>starting()");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        // 可以获取当前机器的操作系统等信息
        Object mes = environment.getSystemProperties().get("os.name");
        System.out.println("SpringApplicationRunListener——>environmentPrepared()---操作系统:"+mes);
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener——>contextPrepared()");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener——>contextLoaded()");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener——>started()");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("SpringApplicationRunListener——>running()");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("SpringApplicationRunListener——>failed()");
    }
}

2. 编写spring.factories文件
将MyApplicationContextInitializer、MySpringApplicationRunListener通过spring.factories注入到IoC容器中。
resources/META-INF/spring.factories

# Initializers
org.springframework.context.ApplicationContextInitializer=\
com.exa.process.listener.MyApplicationContextInitializer

# Application Listeners
org.springframework.boot.SpringApplicationRunListener=\
com.exa.process.listener.MySpringApplicationRunListener

3. 运行项目后日志输出顺序如下

SpringApplicationRunListener——>starting()
SpringApplicationRunListener——>environmentPrepared()---操作系统:Windows 10
ApplicationContextInitializer——>initialize()
SpringApplicationRunListener——>contextPrepared()
SpringApplicationRunListener——>contextLoaded()
SpringApplicationRunListener——>started()
ApplicationRunner——>run()
CommandLineRunner——>run()
SpringApplicationRunListener——>running()
// 由于项目并没有运行失败所以SpringApplicationRunListener——>failed()并没有运行

相关文章

热门文章

更多