您好,登錄后才能下訂單哦!
這篇文章主要介紹“springboot-007啟動配置的原理是什么”,在日常操作中,相信很多人在springboot-007啟動配置的原理是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”springboot-007啟動配置的原理是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
幾個重要的事件回調機制:
ApplicationContextInitializer | 配置在META-INF/spring.factories |
SpringApplicationRunListener | 配置在META-INF/spring.factories |
ApplicationRunner | 只需要放在ioc容器中 |
CommandLineRunner | 只需要放在ioc容器中 |
創建SpringApplication對象
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); //保存主配置類 this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); //判斷當前項目是否是一個web應用 this.webApplicationType = WebApplicationType.deduceFromClasspath(); //從類路徑下找到META-INF/spring.factories里配置的所有ApplicationContextInitializer,然后保存起來 setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); //從類路徑下找到META-INF/spring.factories里配置的所有ApplicationListener,然后保存起來 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); //從多個主配置類中找到包含main方法的主配置類 this.mainApplicationClass = deduceMainApplicationClass(); }
運行run方法
/** * Run the Spring application, creating and refreshing a new * {@link ApplicationContext}. * @param args the application arguments (usually passed from a Java main method) * @return a running {@link ApplicationContext} */ public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); //從類路徑下找到META-INF/spring.factories里配置的所有SpringApplicationRunListener SpringApplicationRunListeners listeners = getRunListeners(args); //回調所有的SpringApplicationRunListener.starting()方法 listeners.starting(); try { //封裝命令行參數 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //準備環境 // 1. 創建環境完成之后回調所有的SpringApplicationRunListener.environmentPrepared()方法,表示環境已經準備好了 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); //創建ApplicationContext:決定是創建web應用的ioc,還是普通的ioc context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); //準備上下文 // 1. 將environment保存到ioc中; // 2. applyInitializers():回調之前保存的所有ApplicationContextInitializer的initialize()方法 // 3. listeners.contextPrepared():回調之前保存的所有SpringApplicationRunListener的contextPrepared()方法 // 4. listeners.contextLoaded():contextPrepared運行完成之后,回調所有SpringApplicationRunListener的contextLoaded()方法 prepareContext(context, environment, listeners, applicationArguments, printedBanner); //刷新容器,即ioc容器的初始化(如果是web應用,還會初始化嵌入式的tomcat) //掃描、創建、加載所有組件的地方 refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } //回調所有的SpringApplicationRunListener.started()方法 listeners.started(context); //從ioc容器中獲取所有的ApplicationRunner和CommandLineRunner,然后進行回調 //所有的ApplicationRunner優先于所有的CommandLineRunner進行回調 callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } //整個SpringBoot應用啟動完成以后,返回ioc容器 return context; }
SpringBoot啟動流程參考
配置在META-INF/spring.factories
#SpringApplicationRunListener org.springframework.boot.SpringApplicationRunListener=\ com.qiang.springboot.listener.HelloSpringApplicationRunListener ##ApplicationContextInitializer org.springframework.context.ApplicationContextInitializer=\ com.qiang.springboot.listener.HelloApplicationContextInitializer
ApplicationContextInitializer
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { System.out.println("ApplicationContextInitializer...initialize..." + configurableApplicationContext); } }
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 osName = environment.getSystemProperties().get("os.name"); System.out.println("SpringApplicationRunListener...environmentPrepared..." + osName); } @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..."); } }
只需要放在ioc容器中
ApplicationRunner
@Component public class HelloApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner...run..." + args); } }
CommandLineRunner
@Component public class HelloCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner...run..." + Arrays.asList(args)); } }
到此,關于“springboot-007啟動配置的原理是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。