Spring Boot在为开发人员提供更高层次的封装,进而提高开发效率的同时,也为出现问题时如何进行定位带来了一定复杂性与难度。但Spring Boot同时又提供了一些诊断工具来辅助开发与分析,如spring-boot-starter-actuator。本文分享一个基于actuator与IDEA条件断点来定位自动配置未生效的案例。望对类似问题分析与处理提供参考。

欢迎关注我的微信公众号:jboost-ksxy

问题确认

在前文介绍的 Spring Boot从入门到实战:整合通用Mapper简化单表操作 中,我们对druid连接池做了自动配置,并且注入了druid的监控统计功能,如下

但本地运行后通过 http://localhost:8080/druid/index.html 访问时却出现错误,通过浏览器的开发者工具查看该请求返回404,推测上述代码中定义的StatViewServlet未注入成功。我们用actuator来确认下是否如此。在项目中加入spring-boot-starter-actuator,并且application.yml中添加如下配置

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: "*"
  6. exclude: beans,trace
  7. endpoint:
  8. health:
  9. show-details: always

在spring-boot 2.x 版本当中,作为安全性考虑,将actuator 控件中的端口,只默认开放/health 和/info 两个端口,其他端口默认关闭, 因此需要添加如上配置。注意include的值 * 必须加引号,否则无法启动。

重启程序后访问 http://localhost:8080/actuator/conditions 确认上述两个实例化方法未满足@ConditionalOnProperty的条件,从而未执行生效,如图

条件断点

从上面分析确认是因为条件注解 @ConditionalOnProperty(prefix = "spring.datasource.druid", name = "druidServletSettings") 未满足使方法未执行导致。那这个条件为什么没有满足呢,查看application.yml中也做了 spring.datasource.druid.druidServletSettings属性的配置。

当你无法理清头绪,确定问题原因时,那就Debug吧。查看注解@ConditionalOnProperty源码,找到其实现支持类OnPropertyCondition,如下

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.TYPE, ElementType.METHOD})
  3. @Documented
  4. @Conditional({OnPropertyCondition.class})
  5. public @interface ConditionalOnProperty {
  6. String[] value() default {};
  7.  
  8. String prefix() default "";
  9.  
  10. String[] name() default {};
  11.  
  12. String havingValue() default "";
  13.  
  14. boolean matchIfMissing() default false;
  15. }

查看OnPropertyCondition源码,了解它是通过getMatchOutcome方法来判断是否满足注解参数所指定的条件的,如下所示

  1. @Override
  2. public ConditionOutcome getMatchOutcome(ConditionContext context,
  3. AnnotatedTypeMetadata metadata) {
  4. List<AnnotationAttributes> allAnnotationAttributes = annotationAttributesFromMultiValueMap(
  5. metadata.getAllAnnotationAttributes(
  6. ConditionalOnProperty.class.getName()));
  7. List<ConditionMessage> noMatch = new ArrayList<>();
  8. List<ConditionMessage> match = new ArrayList<>();
  9. for (AnnotationAttributes annotationAttributes : allAnnotationAttributes) {
  10. ConditionOutcome outcome = determineOutcome(annotationAttributes,
  11. context.getEnvironment());
  12. (outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage());
  13. }
  14. if (!noMatch.isEmpty()) {
  15. return ConditionOutcome.noMatch(ConditionMessage.of(noMatch));
  16. }
  17. return ConditionOutcome.match(ConditionMessage.of(match));
  18. }

在调用determineOutcome处打断点,调试什么原因导致条件未满足,但是这里是一个for循环,如果for元素过多的话,将可能需要断点阻断很多次才能找到你想要查看的那个元素。所幸IDEA提供了不同类型的断点来处理这类问题,前面 案例解析:使用IDEA异常断点来定位java.lang.ArrayStoreException的问题 我们介绍了异常断点的使用。这里介绍用条件断点来处理这类循环块中的debug问题。

在上述代码for循环中调用determineOutcome行打断点,并在断点上右键,弹出如下窗口

图中Condition框即可输入你要指定的条件,可以直接写java判断表达式代码,并引用该行代码处能访问的变量,如这里我们输入 annotationAttributes.get("name").equals("druidServletSettings"),然后点击Debug窗口的“Resume Program (F9)”按钮,则在不满足指定条件时,断点处将不会被阻断,直到条件满足,这样就能很容易定位到我们想要查看的元素。(当然这里allAnnotationAttributes变量其实只有一个元素,仅仅是为了演示条件变量的使用,当集合元素很多时,使用条件断点就能体会到它的方便之处)

问题定位

通过Debug的方式深入条件注解的判断逻辑(其中循环处可使用条件断点),最终来到如下代码片段

在这里是判断来自所有属性源配置的属性中,是否包含条件注解指定的属性,即spring.datasource.druid.druidServletSettings,由上图可见,spring.datasource.druid.druidServletSettings只是某些属性的前缀,并不存在完全匹配的属性,因此返回false,导致条件不满足。回看注解@ConditionOnProperty的javadoc,

  1. * If the property is not contained in the {@link Environment} at all, the
  2. * {@link #matchIfMissing()} attribute is consulted. By default missing attributes do not
  3. * match.
  4. * <p>
  5. * This condition cannot be reliably used for matching collection properties. For example,
  6. * in the following configuration, the condition matches if {@code spring.example.values}
  7. * is present in the {@link Environment} but does not match if
  8. * {@code spring.example.values[0]} is present.
  9. *

当Environment中不包含该属性时,则看matchIfMissing的值,该值默认为false,如果包含该属性,则再对比属性值与havingValue的值,相等即满足,不等则不满足。并且该条件注解不能用于匹配集合类型属性。上述spring.datasource.druid.druidServletSettings实际上属于一个Map类型,因此不能想当然地认为该注解是只要属性集中某属性名称包含该值即满足。

总结

当难以定位到问题原因时,可以进行Debug,跟踪程序运行的各个步骤,当要在循环中Debug定位到某个元素时,可以用条件断点来实现。@ConditionalOnProperty注解不是存在某属性就行,还需要值相等,并且不适用于集合类型属性。

我的个人博客地址:http://blog.jboost.cn
我的github地址:https://github.com/ronwxy
我的微信公众号:jboost-ksxy

———————————————————————————————————————————————————————————————


欢迎关注我的微信公众号,及时获取最新分享

案例解析:springboot自动配置未生效问题定位(条件断点)的更多相关文章

  1. 小BUG大原理:重写WebMvcConfigurationSupport后SpringBoot自动配置失效

    一.背景 公司的项目前段时间发版上线后,测试反馈用户的批量删除功能报错.正常情况下看起来应该是个小 BUG,可怪就怪在上个版本正常,且此次发版未涉及用户功能的改动.因为这个看似小 BUG 我了解到不少 ...

  2. 小BUG大原理 | 第一篇:重写WebMvcConfigurationSupport后SpringBoot自动配置失效

    一.背景 公司的项目前段时间发版上线后,测试反馈用户的批量删除功能报错.正常情况下看起来应该是个小BUG,可怪就怪在上个版本正常,且此次发版未涉及用户功能的改动.因为这个看似小BUG我了解到不少未知的 ...

  3. SpringBoot自动配置注解原理解析

    1. SpringBoot启动主程序类: @SpringBootApplication public class DemoApplication { public static void main(S ...

  4. springboot自动配置源码解析

    springboot版本:2.1.6.RELEASE SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfig ...

  5. SpringBoot 系列教程自动配置选择生效

    191214-SpringBoot 系列教程自动配置选择生效 写了这么久的 Spring 系列博文,发现了一个问题,之前所有的文章都是围绕的让一个东西生效:那么有没有反其道而行之的呢? 我们知道可以通 ...

  6. SpringBoot自动配置探究

    @SpringBootApplication @SpringBootApplication表示SpringBoot应用,标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就 ...

  7. 如何编写Spring-Boot自动配置

    摘要 本文主要介绍如何把一个spring的项目(特别是一些公共工具类项目),基于spring boot的自动配置的思想封装起来,使其他Spring-Boot项目引入后能够进行快速配置. AutoCon ...

  8. SpringBoot实战之SpringBoot自动配置原理

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  9. 源码学习系列之SpringBoot自动配置(篇一)

    源码学习系列之SpringBoot自动配置源码学习(篇一) ok,本博客尝试跟一下Springboot的自动配置源码,做一下笔记记录,自动配置是Springboot的一个很关键的特性,也容易被忽略的属 ...

随机推荐

  1. Arcgis api for javascript学习笔记(4.5版本) - 获取FeatureLayer中的graphics集合

    在Arcgis api for javascript 3.x 版本中,我们可以直接通过某个FeatureLayer对象中的graphics属性获取要素集合. graphics属性 但是在4.x版本中, ...

  2. [Example of Sklearn] - 分类对比

    refrence :http://cloga.info/python/2014/02/07/classify_use_Sklearn/ 加载数据集 这里我使用pandas来加载数据集,数据集采用kag ...

  3. 全栈工程师之路(二)—— JavaScript(网页前端脚本语言)

    javascript 是可以运行在网页前端的脚本语言,可以基于 html 之上实现更丰富的交互(网页内容的交互显示).异步回调.多线程.定时器.动画等. hello_world.html <ht ...

  4. Qt控件焦点切换

    们日常切换控件,例如QQ登陆的账号和密码输入框就可以通过Tab键切换焦点.  图1 qq切换焦点 Qt中QWidget提供了一个静态方式实现该效果 其中也包含介绍使用 [static] void QW ...

  5. sklearn、theano、TensorFlow 以及 theras 的理解

    sklearn ⇒ 机器学习算法和模型: theras theano TensorFlow 1. 理解模型以及函数,参数返回值的实际意义 一定要注意模型的构造函数,接收的参数列表,以及该模型本身所要解 ...

  6. BSD介绍

    BSD许可证模板  * Copyright (c) 1998, Regents of the University of California  * All rights reserved.    * ...

  7. rc-form(翻译)

    原地址:https://npm.taobao.org/package/rc-form rc-form React 高阶表单控制组件.       开发 npm install npm start op ...

  8. WPF特效-鱼游动动画2

    原文:WPF特效-鱼游动动画2           纯代码撸动画实践2:           原图:(png格式)                                            ...

  9. C#热敏打印图片 串口打印图片

    原文:C#热敏打印图片 串口打印图片 如图,一步一步慢慢调出来的 //串口通信类 public System.IO.Ports.SerialPort serialPort = null; serial ...

  10. AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享

    原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net       杨洋    wpfui.com      ...