• springboot启动配置原理之三(事件监听机制)


    ApplicationContextInitializer

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

    SpringApplicationRunListener

    public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
    
        //必须有的构造器
        public HelloSpringApplicationRunListener(SpringApplication application, String[] args){
    
        }
    
        @Override
        public void starting() {
            System.out.println("SpringApplicationRunListener...starting...");
        }
    
        @Override
        public void environmentPrepared(ConfigurableEnvironment environment) {
            Object o = environment.getSystemProperties().get("os.name");
            System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
        }
    
        @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 finished(ConfigurableApplicationContext context, Throwable exception) {
            System.out.println("SpringApplicationRunListener...finished...");
        }
    }

    以上两个起作用,必须配置在resources下META-INF/spring.factories

    org.springframework.context.ApplicationContextInitializer=
    com.atguigu.springboot.listener.HelloApplicationContextInitializer
    
    org.springframework.boot.SpringApplicationRunListener=
    com.atguigu.springboot.listener.HelloSpringApplicationRunListener

    ApplicationRunner

    //放在ioc容器中即可
    @Component
    public class HelloApplicationRunner implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("ApplicationRunner...run....");
        }
    }

    CommandLineRunner

    @Component
    public class HelloCommandLineRunner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
        }
    }
  • 相关阅读:
    课程作业03-1
    课程作业02
    课程作业01:模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
    《大道至简》第一章读后感,伪代码
    《美国教授写给被开除中国留学生的信》阅读笔记
    《大道至简》读后感
    《需求工程——软件建模与分析》阅读笔记02
    《需求工程——软件建模与分析》阅读笔记01
    微信小游戏开发笔记03
    微信小游戏开发笔记02
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/10746925.html
Copyright © 2020-2023  润新知