问题:
Druid好像不适用于springboot3&jdk17
原因:
springboot3&jdk17基于jakarta.servlet,druid底层依赖javax.servlet
过程
-
先看github:
https://github.com/alibaba/druid/issues
-
issues也搜到了一些对于不支持SpringBoot3的疑问
-
下载源码,看了半天(其实github-code中就可以发现)有两个对应的starter
解决
-
SpringBoot2使用com.alibaba.druid.support.http.StatViewServlet
-
SpringBoot3使用com.alibaba.druid.support.jakarta.StatViewServlet;
-
SpringBoot应该使用下面这两个starter
<!--SpringBoot2--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.9</version> </dependency> <!--SpringBoot3--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-3-starter</artifactId> <version>1.2.18</version> </dependency>
-
使用中也有一定的区别
#druid spring: datasource: username: study password: fei123... #?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/springBoot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源 #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 --- #starter spring: datasource: druid: username: study password: fei123... #?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/springBoot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver db-type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j max-pool-prepared-statement-per-connection-size: 20 use-global-data-source-stat: true filter: stat: merge-sql: true slow-sql-millis: 50 proxy-filters: stat,wall,log4j