事件监听机制
Posted xiaoliangup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了事件监听机制相关的知识,希望对你有一定的参考价值。
启动流程:
1、创建SpringApplication对象
private void initialize(Object[] sources) { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources));//保存主配置类 } this.webEnvironment = deduceWebEnvironment(); setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class));//从类路径下找到META‐INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//从类路径下找到ETA‐INF/spring.factories配置的所有ApplicationListener this.mainApplicationClass = deduceMainApplicationClass();//从多个配置类中找到有main方法的主配置类 }
2、运行run方法
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args);
//获取SpringApplicationRunListeners;从类路径下META‐INF/spring.factories listeners.starting();
//回调所有的获取SpringApplicationRunListener.starting()方法 try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
Banner printedBanner = printBanner(environment); context = createApplicationContext(); analyzers = new FailureAnalyzers(context);
//准备上下文环境;将environment保存到ioc中;而且applyInitializers();
//applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
//回调所有的SpringApplicationRunListener的contextPrepared();
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded()
refreshContext(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
//ApplicationRunner先回调,CommandLineRunner再回调
afterRefresh(context, applicationArguments);
//所有的SpringApplicationRunListener回调finished方法 listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); } }
自定义ApplicationContextInitializer继承ApplicationContextInitializer<ConfigurableApplicationContext>
自定义SpringApplicationRunListener继承SpringApplicationRunListener
配置在Resources(META-INF/spring.factories)如:
org.springframework.context.ApplicationContextInitializer=com.atguigu.springboot.listener.HelloApplicationContextInitializer org.springframework.boot.SpringApplicationRunListener=com.atguigu.springboot.listener.HelloSpringApplicationRunListener
自定义ApplicationRunner、CommandLineRunner只需放入IOC 容器即可。
以上是关于事件监听机制的主要内容,如果未能解决你的问题,请参考以下文章
Java AWT 图形界面编程事件处理机制 ② ( Frame 窗口事件监听器 WindowListener | 代码示例 )
Java AWT 图形界面编程事件处理机制 ② ( Frame 窗口事件监听器 WindowListener | 代码示例 )