1
头图
Recently, I wanted to experience the latest version of SpringBoot. After visiting the official website, I found that the latest version of SpringBoot is already 2.6.4 , and the version update is indeed fast enough. After the previous project was upgraded to version 2.6.4 , it was found that there were many pits, not only the problem of circular dependencies, but even Swagger was useless! Today, I will share with you the upgrade process and fill in these pits!

SpringBoot actual e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall

Talk about SpringBoot version

First of all, let's talk about the version of SpringBoot. The latest version is 2.6.4 , 2.7.x will be released soon, and 2.4.x and below have stopped maintenance. The current mainstream versions should be 2.5.x and 2.6.x . See the table below for details.

upgrade process

Let's upgrade the previous mall-tiny-swagger project to see what pits there are and how to solve them!

add dependencies

First, modify the version number of SpringBoot in pom.xml . Note that starting from the 2.4.x version, SpringBoot no longer uses the .RELEASE suffix.

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

circular dependency

  • After starting the project, since SpringBoot prohibits circular reference, we will encounter the first problem, securityConfig and umsAdminServiceImpl circularly referenced, the specific log is as follows;

  • Specifically, our SecurityConfig references UmsAdminService ;

  • And UmsAdminServiceImpl references PasswordEncoder ;

  • Since SecurityConfig inherits WebSecurityConfigurerAdapter , and the Adapter references PasswordEncoder , this leads to a circular reference.

  • To solve this problem is actually very simple, you can modify application.yml to allow circular references directly, but this method is a bit rude and can be used when there is no other method;
spring:
  main:
    allow-circular-references: true
  • In fact, the circular reference is mainly because Spring does not know which bean to create first before it is disabled. We can use the @Lazy annotation to specify a bean for lazy loading to solve this problem gracefully, such as lazy loading UmsAdminService SecurityConfig

Startup error

  • After starting the SpringBoot application again, a null pointer exception will appear. At first glance, it is a Swagger problem. The original Swagger that is very useful can no longer be used!

  • This problem can be solved by adding the following beans to the configuration class of Swagger;
/**
 * 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);
                }
            }
        };
    }

}

Document cannot be displayed

  • Modify application.yml file, the default path matching policy of MVC is PATH_PATTERN_PARSER , which needs to be modified to ANT_PATH_MATCHER ;
spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  • After starting again, I found that Swagger can be used normally!

chat about springfox

When it comes to Swagger, we generally integrate the tool library provided by springfox in SpringBoot. After reading the official website, the project has not released a new version for nearly two years.

Looking at the version in the Maven repository, it still stays at the previous version 3.0.0 . If there is no new version of springfox, it is estimated that with the update of the SpringBoot version, the compatibility will become worse and worse!

Summarize

Today I took you to experience the process of upgrading SpringBoot to version 2.6.x , which mainly solves the problem of circular dependencies and unusable Swagger. I hope it will help you!

If you want to know more SpringBoot combat skills, you can try this practical project with a full set of tutorials (50K+Star): https://github.com/macrozheng/mall

References

Official website address: https://github.com/springfox/springfox

Project source code address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-swagger2


macrozheng
1.1k 声望1.3k 粉丝