Configuring Alibaba Druid DataSource and Monitoring in Spring Boot
This article explains the fundamentals of Alibaba Druid as a Java database connection pool, shows how to add Maven dependencies, configure properties and filters in Spring Boot, set up monitoring pages, remove built‑in ads, and retrieve runtime statistics via DruidStatManagerFacade.
Druid is a high‑performance Java database connection pool developed by Alibaba, offering extensive monitoring and extensibility features that surpass many other pools such as HikariCP, C3P0, and DBCP.
Adding dependencies
<!-- Alibaba Druid starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<!-- MySQL driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Log4j2 for logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Exclude default HikariCP when using MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>Configuring Druid in application.yml
spring:
datasource:
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver # mysql8 driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
minIdle: 10
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 2000
min-evictable-idle-time-millis: 600000
max-evictable-idle-time-millis: 900000
validationQuery: select 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
keepAlive: true
aop-patterns: "com.springboot.template.dao.*"
filters: stat,wall,log4j2
filter:
stat:
enabled: true
db-type: mysql
log-slow-sql: true
slow-sql-millis: 2000
slf4j:
enabled: true
statement-log-error-enabled: true
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 1000
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
login-username: root
login-password: 123
allow: 127.0.0.1The above properties map to com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties and org.springframework.boot.autoconfigure.jdbc.DataSourceProperties , allowing full control over pool size, validation, prepared‑statement caching, and built‑in filters such as StatFilter , WallFilter , and Log4j2Filter .
Enabling and customizing filters
# Enable StatFilter
spring.datasource.druid.filter.stat.enabled=true
spring.datasource.druid.filter.stat.db-type=h2
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# Enable WallFilter
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=h2
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=falseSupported filters include StatFilter, WallFilter, ConfigFilter, EncodingConvertFilter, Slf4jLogFilter, Log4jFilter, Log4j2Filter, and CommonsLogFilter. Custom filters can be added by setting the corresponding enabled flag to true .
Monitoring pages
After starting the application, visit /druid/login.html (default credentials: root / 123) to see:
DataSource overview with pool statistics.
SQL monitoring showing execution time, count, and slow‑SQL logs.
URL monitoring for controller request counts.
Spring AOP‑based monitoring of DAO methods.
SQL firewall (WallFilter) status.
Session monitoring details.
JSON API for programmatic access.
Removing the built‑in advertisement
Two approaches are provided:
Manually comment out the line // this.buildFooter(); in the common.js located inside the Druid JAR ( com/alibaba/druid/1.1.23/druid-1.1.23.jar!/support/http/resources/js/common.js ).
Register a servlet filter that intercepts the common.js request and strips the ad HTML using regular expressions (see the RemoveDruidAdConfig example in the source).
Both methods effectively hide the footer advertisement, with the first being the cleaner solution.
Programmatic access to monitoring data
@RestController
@RequestMapping("/druid")
public class DruidStatController {
@GetMapping("/stat")
public Object druidStat(){
// Retrieve runtime statistics for all data sources
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
}
}The DruidStatManagerFacade#getDataSourceStatDataList method returns a list of DataSourceStatData objects containing connection counts, SQL execution metrics, and more.
In summary, the article provides a complete guide to integrating Alibaba Druid with Spring Boot, configuring its extensive monitoring capabilities, customizing or removing built‑in filters and advertisements, and accessing runtime statistics both via the web UI and programmatically.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.