可以。Spring Boot 4 建议直接用 com.alibaba:druid-spring-boot-4-starter,不要再用 Boot 2 的 druid-spring-boot-starter 或 Boot 3 的 druid-spring-boot-3-starter。Druid 1.2.28 的发布说明明确写了支持 Spring Boot 4.x,并且 Maven Central 已提供 druid-spring-boot-4-starter:1.2.28。(GitHub)
1. Maven 依赖示例
Spring Boot 4 的 Web MVC 新 starter 是 spring-boot-starter-webmvc,旧的 spring-boot-starter-web 仍可用但已标记为推荐迁移到 webmvc;JDBC starter 仍是 spring-boot-starter-jdbc。(Home)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.6</version>
<relativePath/>
</parent>
<dependencies>
<!-- Spring MVC,Boot 4 推荐用 webmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<!-- JDBC / JdbcTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Druid for Spring Boot 4 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-4-starter</artifactId>
<version>1.2.28</version>
</dependency>
<!-- MySQL 驱动,按你的数据库替换 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
如果你用 JPA,可以把 spring-boot-starter-jdbc 换成 spring-boot-starter-data-jpa;如果用 MyBatis,再额外加 MyBatis 的 starter 即可。
2. application.yml 配置 Druid
Druid starter 支持通过 spring.datasource.druid.* 配置 JDBC、连接池、WebStatFilter、StatViewServlet、StatFilter、WallFilter 等;Druid 文档也说明 StatViewServlet 默认不开启,开启后建议设置密码或白名单。(GitHub)
server:
port: 8080
spring:
datasource:
# 显式指定 Druid,避免被默认 Hikari 抢走
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: 123456
druid:
# 基础连接池配置
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
# 检测连接是否有效
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
# 启用内置 filter:stat 监控、wall 防 SQL 注入、slf4j 日志
filters: stat,wall,slf4j
# 慢 SQL 监控
filter:
stat:
enabled: true
db-type: mysql
log-slow-sql: true
slow-sql-millis: 1000
wall:
enabled: true
db-type: mysql
config:
drop-table-allow: false
# Web-JDBC 关联监控
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
# Druid 内置监控页面
stat-view-servlet:
enabled: true
url-pattern: /druid/*
login-username: admin
login-password: change-me
reset-enable: false
# 生产环境建议只允许内网或指定 IP
allow: 127.0.0.1
启动后访问:
/druid/index.html
登录账号密码就是上面配置的:
admin / change-me
生产环境不要把 /druid/* 直接暴露到公网;至少要设置强密码、IP 白名单,或者只在内网/测试环境启用。
3. 写一个接口验证数据库连接
package com.example.demo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
import java.util.Map;
@RestController
public class DbTestController {
private final JdbcTemplate jdbcTemplate;
private final DataSource dataSource;
public DbTestController(JdbcTemplate jdbcTemplate, DataSource dataSource) {
this.jdbcTemplate = jdbcTemplate;
this.dataSource = dataSource;
}
@GetMapping("/db/ping")
public Map<String, Object> ping() {
Map<String, Object> result = jdbcTemplate.queryForMap("SELECT 1 AS ok");
result.put("dataSource", dataSource.getClass().getName());
return result;
}
}
访问:
/db/ping
返回类似:
{
"ok": 1,
"dataSource": "com.alibaba.druid..."
}
只要 dataSource 里能看到 Druid 相关类名,就说明当前数据源已经是 Druid。
4. Spring Boot 4 特别注意点
Spring Boot 4 做了模块化拆分,很多自动配置类包名也变了;例如 DataSourceAutoConfiguration 在 Boot 4 的 API 中位于 org.springframework.boot.jdbc.autoconfigure 包。(Home)
所以如果你用了旧的 Druid starter,可能会遇到类似错误:
ClassNotFoundException: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Druid 的 issue 里也有人在 Boot 4 里使用 druid-spring-boot-3-starter 时遇到这个问题。(GitHub)
正确做法是:
<artifactId>druid-spring-boot-4-starter</artifactId>
而不是:
<artifactId>druid-spring-boot-starter</artifactId> <artifactId>druid-spring-boot-3-starter</artifactId>
5. 常见配置怎么理解
initial-size 是启动时初始化的连接数;min-idle 是最小空闲连接数;max-active 是最大活跃连接数;max-wait 是获取连接的最大等待时间,单位毫秒。
validation-query: SELECT 1 用来检测连接是否可用。MySQL、PostgreSQL 一般可以用 SELECT 1;Oracle 常用 SELECT 1 FROM DUAL。
filters: stat,wall,slf4j 中,stat 用于 SQL 统计和慢 SQL,wall 用于 SQL 防火墙,slf4j 用于日志输出。Druid 文档也说明,如果要让自定义 Filter 配置生效,需要将对应 Filter 的 enabled 设置为 true。(GitHub)
整体流程就是:加 Boot 4 版 Druid starter → 配置 spring.datasource.druid.* → 启动后访问业务接口和 /druid/index.html 验证。