博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot系列--启动方法prepareContext
阅读量:4135 次
发布时间:2019-05-25

本文共 3060 字,大约阅读时间需要 10 分钟。

参考版本:2.0.8.RELEASE

启动方法run中prepareContext方法的执行

/** * 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
exceptionReporters = new ArrayList<>(); configureHeadlessProperty();//启动Spring应用运行监听器 SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args);//配置环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment);//打印banner Banner printedBanner = printBanner(environment);//创建ApplicationContext,并确定类型 context = createApplicationContext();//加载异常报告器类并实例化 exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);//Spring上下文前置处理 prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } listeners.started(context); 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); } return context;}
private void prepareContext(ConfigurableApplicationContext context,      ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,      ApplicationArguments applicationArguments, Banner printedBanner) {   context.setEnvironment(environment);   postProcessApplicationContext(context);//应用上下文的初始化器,执行一些初始化操作   applyInitializers(context);//   listeners.contextPrepared(context);   if (this.logStartupInfo) {//打印启动日志信息      logStartupInfo(context.getParent() == null);      logStartupProfileInfo(context);   }   // Add boot specific singleton beans注册两个特殊的单例   context.getBeanFactory().registerSingleton("springApplicationArguments",         applicationArguments);   if (printedBanner != null) {      context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);   }   // Load the sources   Set sources = getSources();   Assert.notEmpty(sources, "Sources must not be empty");   load(context, sources.toArray(new Object[sources.size()]));   listeners.contextLoaded(context);}

 关于初始化器的解释可参见,回头再自己整理

 

 

转载地址:http://wgivi.baihongyu.com/

你可能感兴趣的文章
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
C++ unique
查看>>
C++ sort
查看>>
C++ merge
查看>>
C++ set_union,set_intersection,set_difference
查看>>
第一章:左旋转字符串
查看>>
程序员编程艺术系列
查看>>
第二章:字符串是否包含问题
查看>>
简洁的heap代码
查看>>
最大团问题
查看>>
C++ make_heap,push_heap,pop_heap,sort_heap(以最大的K个数为例)
查看>>
第三章:寻找最小的k个数
查看>>
第三章续:O(n)复杂度算法
查看>>
Hash算法
查看>>
第三章再续:伴随数组求数组中给定下标区间内的第K小(大)元素
查看>>
第四章:一些字符串函数的实现
查看>>
第五章:寻找满足和为定值的两个或多个数
查看>>
动态规划6:最长递增子序列问题
查看>>