最近升级jdk21+springboot3写基础组件时,碰到druid监控页面配置失效启动会报错:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'statViewServlet' defined in com.shunyu.dma.iot.center.RunApplication: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'statViewServlet' threw exception with message: class com.alibaba.druid.support.http.StatViewServlet cannot be cast to class jakarta.servlet.Servlet (com.alibaba.druid.support.http.StatViewServlet and jakarta.servlet.Servlet are in unnamed module of loader 'app')

由于使用的是springboot3

出现这个启动报错

SpringBoot切换了一些版本依赖,将javax切换到了jakarta

https://github.com/alibaba/druid/issues/5022

由于druid-spring-boot-3-starter的1.2.23版本内部使用的是druid的1.2.15版本,这个版本没兼容jakarta

<!--Druid Spring Boot 组件-->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid-spring-boot-3-starter</artifactId>
          <version>1.2.23</version>
          <exclusions>
              <exclusion>
                  <artifactId>druid</artifactId>
                  <groupId>com.alibaba</groupId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.2.23</version>
      </dependency>
import com.alibaba.druid.support.jakarta.StatViewServlet;

@Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet, "/druid/*");
        return statViewServletServletRegistrationBean;
    }

maven父子依赖传递问题

基础组件父pom定义了版本,虽然上面排除了, 但是还是会被固定, 所以注释或者重新指定版本即可