SpringBoot启动应用及回调监听原理解析
这篇文章主要介绍了SpringBoot启动应用及回调监听原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主类Main方法
publicstaticvoidmain(String[]args){ SpringApplication.run(SpringBootRunApplication.class,args); }
创建SpringApplication对象
publicstaticConfigurableApplicationContextrun(Class>[]primarySources,String[]args){ return(newSpringApplication(primarySources)).run(args); }
构造器
publicSpringApplication(ResourceLoaderresourceLoader,Class>...primarySources){ this.sources=newLinkedHashSet(); this.bannerMode=Mode.CONSOLE; this.logStartupInfo=true; this.addCommandLineProperties=true; this.addConversionService=true; this.headless=true; this.registerShutdownHook=true; this.additionalProfiles=newHashSet(); this.isCustomEnvironment=false; this.lazyInitialization=false; this.resourceLoader=resourceLoader; Assert.notNull(primarySources,"PrimarySourcesmustnotbenull"); this.primarySources=newLinkedHashSet(Arrays.asList(primarySources)); this.webApplicationType=WebApplicationType.deduceFromClasspath(); this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass=this.deduceMainApplicationClass(); }
ApplicationContextInitializer&ApplicationListener
读取META-INF/spring.factories文件中的类
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
执行run方法
publicConfigurableApplicationContextrun(String...args){ StopWatchstopWatch=newStopWatch(); stopWatch.start(); //IOC容器 ConfigurableApplicationContextcontext=null; CollectionexceptionReporters=newArrayList(); this.configureHeadlessProperty(); //读取META-INF/spring.factories文件获得SpringApplicationRunListener的实现类集合 SpringApplicationRunListenerslisteners=this.getRunListeners(args); //监听开始,回调所有SpringApplicationRunListener的starting()方法 listeners.starting(); CollectionexceptionReporters; try{ ApplicationArgumentsapplicationArguments=newDefaultApplicationArguments(args); //监听配置环境准备好了,回调所有SpringApplicationRunListener的environmentPrepared()方法 ConfigurableEnvironmentenvironment=this.prepareEnvironment(listeners,applicationArguments); this.configureIgnoreBeanInfo(environment); BannerprintedBanner=this.printBanner(environment); //创建IOC容器 context=this.createApplicationContext(); exceptionReporters=this.getSpringFactoriesInstances(SpringBootExceptionReporter.class,newClass[]{ConfigurableApplicationContext.class},context); //回调ApplicationContextInitializer的initialize()方法,回调SpringApplicationRunListener的contextPrepared()方法 //回调SpringApplicationRunListener的contextLoaded()方法 this.prepareContext(context,environment,listeners,applicationArguments,printedBanner); //刷新IOC容器,注入组件 this.refreshContext(context); this.afterRefresh(context,applicationArguments); stopWatch.stop(); if(this.logStartupInfo){ (newStartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(),stopWatch); } //回调SpringApplicationRunListener的started()方法 listeners.started(context); //从IOC容器中获得ApplicationRunner、CommandLineRunner的所有组件,回调ApplicationRunner、CommandLineRunner的run()方法 this.callRunners(context,applicationArguments); }catch(Throwablevar10){ this.handleRunFailure(context,var10,exceptionReporters,listeners); thrownewIllegalStateException(var10); } try{ //回调SpringApplicationRunListener的running()方法 listeners.running(context); returncontext; }catch(Throwablevar9){ this.handleRunFailure(context,var9,exceptionReporters,(SpringApplicationRunListeners)null); thrownewIllegalStateException(var9); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。