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

网站建设文件男科治疗价目表

网站建设文件,男科治疗价目表,建工集团官网,聊城那里做网站基于Spring Boot 3.1.0 系列文章 Spring Boot 源码阅读初始化环境搭建Spring Boot 框架整体启动流程详解Spring Boot 系统初始化器详解Spring Boot 监听器详解Spring Boot banner详解Spring Boot 属性配置解析Spring Boot 属性加载原理解析Spring Boot 异常报告器解析 创建自定… 基于Spring Boot 3.1.0 系列文章 Spring Boot 源码阅读初始化环境搭建Spring Boot 框架整体启动流程详解Spring Boot 系统初始化器详解Spring Boot 监听器详解Spring Boot banner详解Spring Boot 属性配置解析Spring Boot 属性加载原理解析Spring Boot 异常报告器解析 创建自定义异常报告器 FailureAnalysis 是Spring Boot 启动时将异常转化为可读消息的一种方法系统自定义了很多异常报告器通过接口也可以自定义异常报告器。 创建一个异常类 public class MyException extends RuntimeException{ }创建一个FailureAnalyzer public class MyFailureAnalyzer extends AbstractFailureAnalyzerMyException {Overrideprotected FailureAnalysis analyze(Throwable rootFailure, MyException cause) {String des 发生自定义异常;String action 由于自定义了一个异常;return new FailureAnalysis(des, action, rootFailure);} }需要在Spring Boot 启动的时候抛出异常为了测试我们在上下文准备的时候抛出自定义异常添加到demo中的MyApplicationRunListener中。 public void contextPrepared(ConfigurableApplicationContext context) {System.out.println(在创建和准备ApplicationContext之后但在加载源之前调用);throw new MyException(); }启动后就会打印出我们的自定义异常报告器内容 *************************** APPLICATION FAILED TO START ***************************Description:发生自定义异常Action:由于自定义了一个异常原理分析 在之前的文章《Spring Boot 框架整体启动流程详解》有讲到过Spring Boot 对异常的处理如下是Spring Boot 启动时的代码 public ConfigurableApplicationContext run(String... args) {long startTime System.nanoTime();DefaultBootstrapContext bootstrapContext createBootstrapContext();ConfigurableApplicationContext context null;configureHeadlessProperty();SpringApplicationRunListeners listeners getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments new DefaultApplicationArguments(args);ConfigurableEnvironment environment prepareEnvironment(listeners, bootstrapContext, applicationArguments);Banner printedBanner printBanner(environment);context createApplicationContext();context.setApplicationStartup(this.applicationStartup);prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);refreshContext(context);afterRefresh(context, applicationArguments);Duration timeTakenToStartup Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);}listeners.started(context, timeTakenToStartup);callRunners(context, applicationArguments);}catch (Throwable ex) {if (ex instanceof AbandonedRunException) {throw ex;}handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {if (context.isRunning()) {Duration timeTakenToReady Duration.ofNanos(System.nanoTime() - startTime);listeners.ready(context, timeTakenToReady);}}catch (Throwable ex) {if (ex instanceof AbandonedRunException) {throw ex;}handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;}通过两个try…catch…包裹在catch 中判断异常是否是AbandonedRunException类型是直接抛出异常否则的话进入handleRunFailure中。 AbandonedRunException 异常 在 Spring Boot 处理AOT相关优化的时候会抛出 private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,SpringApplicationRunListeners listeners) {try {try {//处理exitCodehandleExitCode(context, exception);if (listeners ! null) {//发送启动失败事件listeners.failed(context, exception);}}finally {//获取报告处理器并处理错误reportFailure(getExceptionReporters(context), exception);if (context ! null) {//关闭上下文context.close();//移除关闭钩子shutdownHook.deregisterFailedApplicationContext(context);}}}catch (Exception ex) {logger.warn(Unable to close ApplicationContext, ex);}//重新抛出异常ReflectionUtils.rethrowRuntimeException(exception); }exitCode是一个整数值默认返回0Spring Boot会将该exitCode传递给System.exit()以作为状态码返回如下是IDEA中停止Spring Boot 返回的退出码 进程已结束,退出代码130 handleExitCode 进入handleExitCode看下是如何处理的 private void handleExitCode(ConfigurableApplicationContext context, Throwable exception) {int exitCode getExitCodeFromException(context, exception);//exitCode非0if (exitCode ! 0) {if (context ! null) {//发送ExitCodeEvent事件context.publishEvent(new ExitCodeEvent(context, exitCode));}//获取当前线程的SpringBootExceptionHandlerSpringBootExceptionHandler用来处理未捕获的异常实现了UncaughtExceptionHandler接口handler getSpringBootExceptionHandler();if (handler ! null) {//添加exitCode到SpringBootExceptionHandler 中handler.registerExitCode(exitCode);}} }private int getExitCodeFromException(ConfigurableApplicationContext context, Throwable exception) { //从ExitCodeExceptionMapper实现中获取exitCodeint exitCode getExitCodeFromMappedException(context, exception);if (exitCode 0) {//尝试从ExitCodeGenerator实现获取exitCodeexitCode getExitCodeFromExitCodeGeneratorException(exception);}return exitCode; }private int getExitCodeFromMappedException(ConfigurableApplicationContext context, Throwable exception) { //判断上下文是否是活动状态上下文至少刷新过一次不是就返回0if (context null || !context.isActive()) {return 0;}//用于维护ExitCodeGenerator有序集合的组合器ExitCodeGenerator 是一个接口用于获取exitCodeExitCodeGenerators generators new ExitCodeGenerators();//获取ExitCodeExceptionMapper类型的BeanCollectionExitCodeExceptionMapper beans context.getBeansOfType(ExitCodeExceptionMapper.class).values();//将异常和bean包装成MappedExitCodeGenerator,排序后保存MappedExitCodeGenerator是ExitCodeGenerator 的一个实现generators.addAll(exception, beans);//会循环ExitCodeGenerators 中的ExitCodeGeneratorExitCodeGenerator会去获取ExitCodeExceptionMapper的实现如果有一个exitCode非0则马上返回否则返回0return generators.getExitCode(); }private int getExitCodeFromExitCodeGeneratorException(Throwable exception) { //没有异常if (exception null) {return 0;}//异常类有实现了ExitCodeGenerator 接口if (exception instanceof ExitCodeGenerator generator) {return generator.getExitCode();}//继续寻找return getExitCodeFromExitCodeGeneratorException(exception.getCause()); }SpringBootExceptionHandler getSpringBootExceptionHandler() { //当前线程是主线程if (isMainThread(Thread.currentThread())) {//获取当前线程的SpringBootExceptionHandlerreturn SpringBootExceptionHandler.forCurrentThread();}return null; }listeners.failed 在处理完exitCode后继续执行listeners.failed(context, exception)这里就跟以前一样循环SpringApplicationRunListener实现 reportFailure Spring Boot 首先从spring.factories获取所有的SpringBootExceptionReporter实现FailureAnalyzers是其唯一实现其用于加载和执行FailureAnalyzer reportFailure 循环执行获取的SpringBootExceptionReporter如果发送异常成功则会向之前的SpringBootExceptionHandler中记录表示该异常已经捕获处理 private void reportFailure(CollectionSpringBootExceptionReporter exceptionReporters, Throwable failure) {try {for (SpringBootExceptionReporter reporter : exceptionReporters) {//如果异常发送成功if (reporter.reportException(failure)) {//记录异常registerLoggedException(failure);return;}}}catch (Throwable ex) {// 如果上述操作发生异常还是会继续执行}//记录error级别日志if (logger.isErrorEnabled()) {logger.error(Application run failed, failure);registerLoggedException(failure);} }reporter.reportException 在reportFailure中通过reporter.reportException(failure)判断异常是否发送成功进入代码由于该Demo 只有一个FailureAnalyzers实现所以进入到FailureAnalyzers的reportException中 public boolean reportException(Throwable failure) { //循环调用加载的FailureAnalyzer实现的analyze方法FailureAnalysis analysis analyze(failure, this.analyzers);//加载FailureAnalysisReporter实现组装具体错误信息并打印日志return report(analysis); }this.analyzers在FailureAnalyzers创建的时候已经将FailureAnalyzer实现从spring.factories中加载 下面的代码将循环调用加载的FailureAnalyzer实现的analyze方法返回一个包装了异常描述、发生异常的动作、原始异常 信息的对象 private FailureAnalysis analyze(Throwable failure, ListFailureAnalyzer analyzers) {for (FailureAnalyzer analyzer : analyzers) {try {FailureAnalysis analysis analyzer.analyze(failure);if (analysis ! null) {return analysis;}}catch (Throwable ex) {logger.trace(LogMessage.format(FailureAnalyzer %s failed, analyzer), ex);}}return null; }此处Spring Boot 建议自定义的FailureAnalyzer 通过继承AbstractFailureAnalyzer来实现Spring Boot 自带的FailureAnalyzer确实也是这样的但是你也可以直接实现FailureAnalyzer 接口。AbstractFailureAnalyzer中会筛选出需要关注的异常而直接实现FailureAnalyzer 接口需要自行在方法中处理。 随后将返回的FailureAnalysis实现通过FailureAnalysisReporter组装打印到客户端 private boolean report(FailureAnalysis analysis) { //FailureAnalysisReporter也是从spring.factories中加载可见也可以自定义ListFailureAnalysisReporter reporters this.springFactoriesLoader.load(FailureAnalysisReporter.class);if (analysis null || reporters.isEmpty()) {return false;}for (FailureAnalysisReporter reporter : reporters) {reporter.report(analysis);}return true; }在该Demo中只有一个FailureAnalysisReporter实例LoggingFailureAnalysisReporter public void report(FailureAnalysis failureAnalysis) { //如果是debug级别则会打印堆栈信息if (logger.isDebugEnabled()) {logger.debug(Application failed to start due to an exception, failureAnalysis.getCause());}//如果是error级别还会打印组装好的错误信息if (logger.isErrorEnabled()) {logger.error(buildMessage(failureAnalysis));} }private String buildMessage(FailureAnalysis failureAnalysis) {StringBuilder builder new StringBuilder();builder.append(String.format(%n%n));builder.append(String.format(***************************%n));builder.append(String.format(APPLICATION FAILED TO START%n));builder.append(String.format(***************************%n%n));builder.append(String.format(Description:%n%n));builder.append(String.format(%s%n, failureAnalysis.getDescription()));if (StringUtils.hasText(failureAnalysis.getAction())) {builder.append(String.format(%nAction:%n%n));builder.append(String.format(%s%n, failureAnalysis.getAction()));}return builder.toString(); }关闭上下文、移除钩子 context.close() 如果上下文不为空则关闭上下文并且移除关闭钩子。 shutdownHook.deregisterFailedApplicationContext(context) 用来将之前在SpringApplicationShutdownHook 钩子中注册的上下文移除。 SpringApplicationShutdownHook 是Spring Boot 定义的关闭钩子用来优雅关机。 总结
http://www.hkea.cn/news/14588721/

相关文章:

  • asp制作网站搜索引擎优化课程
  • 怎么免费发布网站网站做seo 反应非常慢
  • 群艺馆网站建设方案官方网站aspcms
  • 免费的企业网站建设流程上海免费关键词排名优化
  • 巴音郭楞蒙古自治州建设局网站嘉兴平湖网站建设
  • 网站建设 大纲如何建设自己的公司网站
  • 网站推广引流软件学校建设网站目标
  • 下载软件的appseo网络推广优化教程
  • 网站建设扌金手指六六在招聘网站做电话销售怎么样
  • .net 网站开发框架牡丹江seo
  • 郑州网站开发公江门网站建设推广策划
  • 衡阳网站seo优化百度推广授权代理商
  • 搞笑视频网站建设策划书网页制作与网站建设实战大全 pdf下载
  • 海外网站搭建芜湖做网站优化
  • 上海电子商城网站制作海丰建设局网站
  • 网站取消301后久久建筑网施工方案好用吗
  • 成都设计公司工资多少云南seo简单整站优化
  • 现在有没有免费的网站空间绍兴公司企业名单
  • 牛网站建设想做网站的公司好
  • 建站公司 知乎 discuz商务网站内容维护范围
  • 做软件的叫什么职业网站标题具体怎样优化
  • 安平县护栏网站建设桌面百度
  • 宝华路桥建设集团网站wordpress 如何汉化主题
  • 网站制作 公司资质wordpress分类下文章置顶
  • python做的网站如何部署做淘宝内部优惠券网站要钱么
  • 网站值不值得做seo2023年楼市将迎来抛售潮
  • 网址导航网站可信软件开发工程师
  • 网站备案在哪查八面通网站建设
  • 青岛网站建设排名微信微网站开发报价单
  • 各大网站搜索引擎入口互联网保险产品