当前位置: 首页 > news >正文

网站开发 手机 电脑莆田有交做外贸网站的没

网站开发 手机 电脑,莆田有交做外贸网站的没,房产证查询系统官方网站,淘宝客网站源码html转载自 Spring Boot 2.x 启动全过程源码分析#xff08;全#xff09; 上篇《Spring Boot 2.x 启动全过程源码分析#xff08;一#xff09;入口类剖析》我们分析了 Spring Boot 入口类 SpringApplication 的源码#xff0c;并知道了其构造原理#xff0c;这篇我们继…转载自    Spring Boot 2.x 启动全过程源码分析全 上篇《Spring Boot 2.x 启动全过程源码分析一入口类剖析》我们分析了 Spring Boot 入口类 SpringApplication 的源码并知道了其构造原理这篇我们继续往下面分析其核心 run 方法。 SpringApplication 实例 run 方法运行过程 上面分析了 SpringApplication 实例对象构造方法初始化过程下面继续来看下这个 SpringApplication 对象的 run 方法的源码和运行流程。 public ConfigurableApplicationContext run(String... args) {// 1、创建并启动计时监控类StopWatch stopWatch  new StopWatch();stopWatch.start();// 2、初始化应用上下文和异常报告集合ConfigurableApplicationContext context  null;CollectionSpringBootExceptionReporter exceptionReporters  new ArrayList();// 3、设置系统属性 java.awt.headless 的值默认值为trueconfigureHeadlessProperty();// 4、创建所有 Spring 运行监听器并发布应用启动事件SpringApplicationRunListeners listeners  getRunListeners(args);listeners.starting();try {// 5、初始化默认应用参数类ApplicationArguments applicationArguments  new DefaultApplicationArguments(args);// 6、根据运行监听器和应用参数来准备 Spring 环境ConfigurableEnvironment environment  prepareEnvironment(listeners,applicationArguments);configureIgnoreBeanInfo(environment);// 7、创建 Banner 打印类Banner printedBanner  printBanner(environment);// 8、创建应用上下文context  createApplicationContext();// 9、准备异常报告器exceptionReporters  getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);// 10、准备应用上下文prepareContext(context, environment, listeners, applicationArguments,printedBanner);// 11、刷新应用上下文refreshContext(context);// 12、应用上下文刷新后置处理afterRefresh(context, applicationArguments);// 13、停止计时监控类stopWatch.stop();// 14、输出日志记录执行主类名、时间信息if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 15、发布应用上下文启动完成事件listeners.started(context);// 16、执行所有 Runner 运行器callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try {// 17、发布应用上下文就绪事件listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}// 18、返回应用上下文return context; }所以我们可以按以下几步来分解 run 方法的启动过程。 1、创建并启动计时监控类 StopWatch stopWatch  new StopWatch(); stopWatch.start();来看下这个计时监控类 StopWatch 的相关源码 public void start() throws IllegalStateException {start(); }public void start(String taskName) throws IllegalStateException {if (this.currentTaskName ! null) {throw new IllegalStateException(Cant start StopWatch: its already running);}this.currentTaskName  taskName;this.startTimeMillis  System.currentTimeMillis(); }首先记录了当前任务的名称默认为空字符串然后记录当前 Spring Boot 应用启动的开始时间。 2、初始化应用上下文和异常报告集合 ConfigurableApplicationContext context  null; CollectionSpringBootExceptionReporter exceptionReporters  new ArrayList();3、设置系统属性 java.awt.headless 的值 configureHeadlessProperty();设置该默认值为trueJava.awt.headless true 有什么作用 对于一个 Java 服务器来说经常要处理一些图形元素例如地图的创建或者图形和图表等。这些API基本上总是需要运行一个X-server以便能使用AWTAbstract Window Toolkit抽象窗口工具集。然而运行一个不必要的 X-server 并不是一种好的管理方式。有时你甚至不能运行 X-server,因此最好的方案是运行 headless 服务器来进行简单的图像处理。 参考www.cnblogs.com/princessd8251/p/4000016.html 4、创建所有 Spring 运行监听器并发布应用启动事件 SpringApplicationRunListeners listeners  getRunListeners(args); listeners.starting();来看下创建 Spring 运行监听器相关的源码 private SpringApplicationRunListeners getRunListeners(String[] args) {Class?[] types  new Class?[] { SpringApplication.class, String[].class };return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args)); }SpringApplicationRunListeners(Log log,Collection? extends SpringApplicationRunListener listeners) {this.log  log;this.listeners  new ArrayList(listeners); }创建逻辑和之前实例化初始化器和监听器的一样一样调用的是 getSpringFactoriesInstances 方法来获取配置的监听器名称并实例化所有的类。 SpringApplicationRunListener 所有监听器配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。 # Run Listeners org.springframework.boot.SpringApplicationRunListener\ org.springframework.boot.context.event.EventPublishingRunListener5、初始化默认应用参数类 ApplicationArguments applicationArguments  new DefaultApplicationArguments(args);6、根据运行监听器和应用参数来准备 Spring 环境 ConfigurableEnvironment environment  prepareEnvironment(listeners,applicationArguments); configureIgnoreBeanInfo(environment);下面我们主要来看下准备环境的 prepareEnvironment 源码 private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments) {// 6.1) 获取或者创建应用环境ConfigurableEnvironment environment  getOrCreateEnvironment();// 6.2) 配置应用环境configureEnvironment(environment, applicationArguments.getSourceArgs());listeners.environmentPrepared(environment);bindToSpringApplication(environment);if (this.webApplicationType  WebApplicationType.NONE) {environment  new EnvironmentConverter(getClassLoader()).convertToStandardEnvironmentIfNecessary(environment);}ConfigurationPropertySources.attach(environment);return environment; }6.1) 获取或者创建应用环境 private ConfigurableEnvironment getOrCreateEnvironment() {if (this.environment ! null) {return this.environment;}if (this.webApplicationType  WebApplicationType.SERVLET) {return new StandardServletEnvironment();}return new StandardEnvironment(); }这里分为标准 Servlet 环境和标准环境。 6.2) 配置应用环境 protected void configureEnvironment(ConfigurableEnvironment environment,String[] args) {configurePropertySources(environment, args);configureProfiles(environment, args); }这里分为以下两步来配置应用环境。 配置 property sources 配置 Profiles 这里主要处理所有 property sources 配置和 profiles 配置。 7、创建 Banner 打印类 Banner printedBanner  printBanner(environment);这是用来打印 Banner 的处理类这个没什么好说的。 8、创建应用上下文 context  createApplicationContext();来看下 createApplicationContext() 方法的源码 protected ConfigurableApplicationContext createApplicationContext() {Class? contextClass  this.applicationContextClass;if (contextClass  null) {try {switch (this.webApplicationType) {case SERVLET:contextClass  Class.forName(DEFAULT_WEB_CONTEXT_CLASS);break;case REACTIVE:contextClass  Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);break;default:contextClass  Class.forName(DEFAULT_CONTEXT_CLASS);}}catch (ClassNotFoundException ex) {throw new IllegalStateException(Unable create a default ApplicationContext,  please specify an ApplicationContextClass,ex);}}return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }其实就是根据不同的应用类型初始化不同的上下文应用类。 9、准备异常报告器 exceptionReporters  getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context);逻辑和之前实例化初始化器和监听器的一样一样调用的是 getSpringFactoriesInstances 方法来获取配置的异常类名称并实例化所有的异常处理类。 该异常报告处理类配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 这个配置文件里面。 # Error Reporters org.springframework.boot.SpringBootExceptionReporter\ org.springframework.boot.diagnostics.FailureAnalyzers10、准备应用上下文 prepareContext(context, environment, listeners, applicationArguments,printedBanner);来看下 prepareContext() 方法的源码 private void prepareContext(ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {// 10.1绑定环境到上下文context.setEnvironment(environment);// 10.2配置上下文的 bean 生成器及资源加载器postProcessApplicationContext(context);// 10.3为上下文应用所有初始化器applyInitializers(context);// 10.4触发所有 SpringApplicationRunListener 监听器的 contextPrepared 事件方法listeners.contextPrepared(context);// 10.5记录启动日志if (this.logStartupInfo) {logStartupInfo(context.getParent()  null);logStartupProfileInfo(context);}// 10.6注册两个特殊的单例beancontext.getBeanFactory().registerSingleton(springApplicationArguments,applicationArguments);if (printedBanner ! null) {context.getBeanFactory().registerSingleton(springBootBanner, printedBanner);}// 10.7加载所有资源SetObject sources  getAllSources();Assert.notEmpty(sources, Sources must not be empty);load(context, sources.toArray(new Object[0]));// 10.8触发所有 SpringApplicationRunListener 监听器的 contextLoaded 事件方法listeners.contextLoaded(context); }11、刷新应用上下文 refreshContext(context);这个主要是刷新 Spring 的应用上下文源码如下不详细说明。 private void refreshContext(ConfigurableApplicationContext context) {refresh(context);if (this.registerShutdownHook) {try {context.registerShutdownHook();}catch (AccessControlException ex) {// Not allowed in some environments.}} }12、应用上下文刷新后置处理 afterRefresh(context, applicationArguments);看了下这个方法的源码是空的目前可以做一些自定义的后置处理操作。 /*** Called after the context has been refreshed.* param context the application context* param args the application arguments*/ protected void afterRefresh(ConfigurableApplicationContext context,ApplicationArguments args) { }13、停止计时监控类 stopWatch.stop();public void stop() throws IllegalStateException {if (this.currentTaskName  null) {throw new IllegalStateException(Cant stop StopWatch: its not running);}long lastTime  System.currentTimeMillis() - this.startTimeMillis;this.totalTimeMillis  lastTime;this.lastTaskInfo  new TaskInfo(this.currentTaskName, lastTime);if (this.keepTaskList) {this.taskList.add(this.lastTaskInfo);}this.taskCount;this.currentTaskName  null; }计时监听器停止并统计一些任务执行信息。 14、输出日志记录执行主类名、时间信息 if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); }15、发布应用上下文启动完成事件 listeners.started(context);触发所有 SpringApplicationRunListener 监听器的 started 事件方法。 16、执行所有 Runner 运行器 callRunners(context, applicationArguments);private void callRunners(ApplicationContext context, ApplicationArguments args) {ListObject runners  new ArrayList();runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());AnnotationAwareOrderComparator.sort(runners);for (Object runner : new LinkedHashSet(runners)) {if (runner instanceof ApplicationRunner) {callRunner((ApplicationRunner) runner, args);}if (runner instanceof CommandLineRunner) {callRunner((CommandLineRunner) runner, args);}} }执行所有 ApplicationRunner 和 CommandLineRunner 这两种运行器不详细展开了。 17、发布应用上下文就绪事件 listeners.running(context);触发所有 SpringApplicationRunListener 监听器的 running 事件方法。 18、返回应用上下文 return context;总结 Spring Boot 的启动全过程源码分析至此分析 Spring 源码真是一个痛苦的过程希望能给大家提供一点参考和思路也希望能给正在 Spring Boot 学习路上的朋友一点收获。
http://www.huolong8.cn/news/278549/

相关文章:

  • 上海网站建设 觉策动力wordpress 增加侧边栏
  • 网站建设进度时间表南京seo网络优化公司
  • 成都便宜网站建设海淀手机网站建设
  • 站长之家alexa排名做外贸需要哪些网站有哪些
  • 那种限时购的网站如何做河源新闻头条最新新闻
  • 设计好的免费网站建设中国外包公司排行榜
  • 深圳网站建设 宝华大厦做网站东莞选哪家公司好
  • 芜湖网站建设推广公司公司宣传册设计样本
  • 一般做个网站多少钱旅游景点网站建设现状
  • 泉州网站建设咨询seo诊断
  • 企业网站酷站seo排名赚app官网
  • 电商网站开发目的wordpress文章打开很慢
  • 做订票网站设计要多久网站建设的一般步骤包括哪些
  • 南京专业网站营销wordpress栏目置顶
  • 分销商城网站开发美御品牌推广
  • 长春启做网站多少网站建设交流论坛
  • 网站开发的需要的技术人员网络平台营销的特点
  • 网站的交互怎么做wordpress做商城
  • 鲜花类网站建设策划书范文赣州专业网站推广多少钱
  • 沧州网站建没wordpress免费主题推荐
  • 宝安附近公司做网站建设哪家效益快0基础1小时网站建设教程
  • 自己电脑上做网站别人访问怎样上百度做广告
  • 做商城网站要多少钱给公司做网站 图片倾权
  • 哪里有做网站福州seo关键字推广
  • 电商服务站点建设方案有哪些好的做网站公司好
  • 网站建设首页网站建设公司选择意见书
  • 网站策划建设方法建立网站需要备案吗
  • 温州做网站的公司有哪些网站运营的提成方案怎么做
  • php中switch做网站宜昌十堰网站建设哪家好
  • 网站开发一次性费用网站建设header