目录
  • 前言
  • 升级过程
    • 添加依赖
    • 循环依赖
    • 启动出错
    • 文档无法显示
  • 聊聊springfox
    • 总结

      前言

      最近想体验下最新版本的SpringBoot,逛了下官网,发现SpringBoot目前最新版本已经是2.6.4了,版本更新确实够快的。之前的项目升级了2.6.4版本后发现有好多坑,不仅有循环依赖的问题,连Swagger都没法用了!今天给大家分享下升级过程,填一填这些坑!

      SpringBoot实战电商项目mall(50k+star)地址:https://github.com/macrozheng/mall

      首先我们来聊聊SpringBoot的版本,目前最新版本是2.6.4版本,2.7.x即将发布,2.4.x及以下版本已经停止维护了,目前的主流版本应该是2.5.x和2.6.x。具体可以看下下面这张表。

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      升级过程

      下面我们将之前的mall-tiny-swagger项目升级下,看看到底有哪些坑,这些坑该如何解决!

      添加依赖

      首先在pom.xml中修改SpringBoot的版本号,注意从2.4.x版本开始,SpringBoot就不再使用.RELEASE后缀了。

      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.6.4</version>
          <relativePath/> <!-- lookup parent from repository -->
      </parent>
      

      循环依赖

      启动项目后,由于SpringBoot禁止了循环引用,我们会遇到第一个问题,securityConfig和umsAdminServiceImpl循环引用了,具体日志如下;

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      具体来说就是我们的SecurityConfig引用了UmsAdminService;

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      而UmsAdminServiceImpl又引用了PasswordEncoder;

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      由于SecurityConfig继承了WebSecurityConfigurerAdapter,而Adapter又引用了PasswordEncoder,这样就导致了循环引用。

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      要解决这个问题其实很简单,你可以修改application.yml直接允许循环引用,不过这个方法有点粗暴,在没有其他方法的时候可以使用;

      spring:
        main:
          allow-circular-references: true
      

      其实循环引用主要是因为会导致Spring不知道该先创建哪个Bean才会被禁用的,我们可以使用@Lazy注解指定某个Bean进行懒加载就可以优雅解决该问题,比如在SecurityConfig中懒加载UmsAdminService。

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      启动出错

      再次启动SpringBoot应用后会出现一个空指针异常,一看就是Swagger问题,原来挺好用的Swagger不能用了!

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      在Swagger的配置类中添加如下Bean可以解决该问题;

      /**
       * Swagger2API文档的配置
       */
      @Configuration
      public class Swagger2Config {
          @Bean
          public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
              return new BeanPostProcessor() {
                  @Override
                  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                      if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                          customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                      }
                      return bean;
                  }
                  private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
                      List<T> copy = mappings.stream()
                              .filter(mapping -> mapping.getPatternParser() == null)
                              .collect(Collectors.toList());
                      mappings.clear();
                      mappings.addAll(copy);
                  }
                  @SuppressWarnings("unchecked")
                  private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                      try {
                          Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                          field.setAccessible(true);
                          return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                      } catch (IllegalArgumentException | IllegalAccessException e) {
                          throw new IllegalStateException(e);
                      }
                  }
              };
          }
      }
      

      文档无法显示

      再次启动后访问Swagger文档,会发现之前好好的文档也无法显示了,访问地址:http://localhost:8088/swagger-ui/

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      修改application.yml文件,MVC默认的路径匹配策略为PATH_PATTERN_PARSER,需要修改为ANT_PATH_MATCHER;

      spring:
        mvc:
          pathmatch:
            matching-strategy: ANT_PATH_MATCHER
      

      再次启动后发现Swagger已经可以正常使用了!

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      聊聊springfox

      提到Swagger,我们一般在SpringBoot中集成的都是springfox给我们提供的工具库,看了下官网,该项目已经快两年没有发布新版本了。

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      再看下Maven仓库中的版本,依旧停留在之前的3.0.0版本。如果springfox再不出新版本的话,估计随着SpringBoot版本的更新,兼容性会越来越差的!

      SpringBoot2.6.x升级后循环依赖及Swagger无法使用问题

      总结

      今天带大家体验了一把SpringBoot升级2.6.x版本的过程,主要解决了循环依赖和Swagger无法使用的问题,希望对大家有所帮助!

      如果你想了解更多SpringBoot实战技巧的话,可以试试这个带全套教程的实战项目(50K+Star):https://github.com/macrozheng/mall

      参考资料

      官网地址:https://github.com/springfox/springfox

      项目源码地址 https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-swagger2

      更多关于SpringBoot2.6.x升级循环依赖Swagger的资料请关注其它相关文章!

      声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。