重写四大组件干预Springboot的启动(菜鸟笔记)

重写四大组件干预Springboot的启动

文章目录

  • 重写四大组件干预Springboot的启动
    • 1. 四大组件
    • 2. 重写ApplicationContextInitializer
    • 3. 重写SpringApplicationRunListener
    • 4. 重写ApplicationRunner
    • 5. 重写CommandLineRunner
    • 6. 各个组件的执行时机

1. 四大组件

//需要在META-INF/spring.factries配置
ApplicationContextInitializer
SpringApplicationRunListener

//需要注入IOC容器
ApplicationRunner
CommandLineRunner

2. 重写ApplicationContextInitializer

package com.learn.boot.bootmaster.listener;

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

public class CustomApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("CustomApplicationContextInitializer...initialize..." + applicationContext);
    }
}

需要在META-INF/spring.factories文件中进行配置

# Initializers
org.springframework.context.ApplicationContextInitializer=\
com.learn.boot.bootmaster.listener.CustomApplicationContextInitializer

3. 重写SpringApplicationRunListener

package com.learn.boot.bootmaster.listener;

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

public class CustomSpringApplicationRunListener implements SpringApplicationRunListener {

    public CustomSpringApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void starting() {
        System.out.println("CustomSpringApplicationRunListener...starting...");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("CustomSpringApplicationRunListener...environmentPrepared..." + environment.getSystemProperties().get("os.name"));
    }

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

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

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

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

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

需要在META-INF/spring.factories文件中进行配置

注坑:是org.springframework.boot.SpringApplicationRunListener,而不是org.springframework.context.SpringApplicationRunListener

# Application Listeners
org.springframework.boot.SpringApplicationRunListener=\
com.learn.boot.bootmaster.listener.CustomSpringApplicationRunListener

4. 重写ApplicationRunner

该组件不需要配置在META-INF/spring.factories文件中,只需要注入IOC容器即可

package com.learn.boot.bootmaster.listener;

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

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

5. 重写CommandLineRunner

package com.learn.boot.bootmaster.listener;

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

import java.util.Arrays;

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

6. 各个组件的执行时机

与上一章springboot启动源码执行顺序结合梳理

重写四大组件干预Springboot的启动(菜鸟笔记)_第1张图片

你可能感兴趣的:(java,springboot)