Spring Boot can use Druid to quickly gain a high-performance database connection pool and visual monitoring capabilities, addressing the weak observability of the default connection pool and the lack of intuitive SQL statistics. This guide covers dependency setup, key configuration points, monitoring enablement, and security hardening. Keywords: Spring Boot, Druid, database monitoring.
Technical Specification Snapshot
| Parameter | Description |
|---|---|
| Core Language | Java |
| Framework | Spring Boot |
| Connection Pool | Alibaba Druid |
| Protocols / Interfaces | JDBC, Servlet |
| Dependency Coordinate | com.alibaba:druid-spring-boot-3-starter:1.2.24 |
| Monitoring Entry | /druid/ |
| Star Count | Not provided in the source; refer to the official GitHub repository |
| Core Dependencies | DruidDataSource, StatViewServlet, WebStatFilter |
Spring Boot integration with Druid improves both connection management and observability
Druid is an open-source database connection pool from Alibaba. In addition to connection reuse and performance optimization, it includes built-in capabilities such as SQL statistics, slow query analysis, and connection pool status visualization.
Compared with solutions that provide only basic pooling, Druid is a better fit for Spring Boot applications that need to troubleshoot SQL performance, monitor connection usage, and identify database bottlenecks.
Adding the Maven dependency is the first step to integrating Druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.24</version>
</dependency>
This configuration adds the official Druid Starter to a Spring Boot 3 project.
implementation 'com.alibaba:druid-spring-boot-3-starter:1.2.24'
This configuration is for projects that use Gradle for dependency management.
Datasource parameters define the capacity and availability of the connection pool
In a real integration, the key is not just replacing the datasource type. You also need to define pool capacity, connection validation, and monitoring switches.
The following properties example is more practical for direct use:
# Specify Druid as the datasource implementation
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# Initial number of connections
spring.datasource.druid.initial-size=5
# Minimum number of idle connections
spring.datasource.druid.min-idle=5
# Maximum number of active connections
spring.datasource.druid.max-active=20
# Validate connection availability when borrowing a connection
spring.datasource.druid.test-on-borrow=true
# Validation query used to verify that the connection is valid
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# Enable the Web statistics filter
spring.datasource.druid.web-stat-filter.enabled=true
# Enable the monitoring Servlet
spring.datasource.druid.stat-view-servlet.enabled=true
# Set the monitoring page login username
spring.datasource.druid.stat-view-servlet.login-username=druid
# Set the monitoring page login password
spring.datasource.druid.stat-view-servlet.login-password=d1234
This configuration initializes the connection pool and enables the monitoring page at the same time.
YAML configuration is better for hierarchical maintenance
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false
username: root
password: 1234
type: com.alibaba.druid.pool.DruidDataSource
druid:
test-on-borrow: true
validation-query: SELECT 1 FROM DUAL
web-stat-filter:
enabled: true
stat-view-servlet:
enabled: true
login-username: druid
login-password: d1234
This YAML example shows the standard hierarchical structure for Druid parameters in Spring Boot.
Enabling the monitoring page essentially means registering the statistics Filter and status Servlet
Druid’s monitoring capability mainly comes from two components: WebStatFilter collects Web and SQL access data, and StatViewServlet renders the visual dashboard.
If you configure only the datasource without enabling these two components, you will not be able to access the /druid/ monitoring endpoint or view SQL statistics.
# Enable SQL and Web statistics collection
spring.datasource.druid.web-stat-filter.enabled=true
# Enable access to the monitoring page
spring.datasource.druid.stat-view-servlet.enabled=true
This minimal configuration is enough to quickly verify that monitoring is working.
A custom configuration class adds access control and filtering rules
When the default auto-configuration does not meet your requirements, you can manually register the Servlet and Filter to restrict IP access, exclude static resources, or adjust the monitoring path.
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DruidDataSource druidDataSource() {
// Bind druid parameters from the configuration file
return new DruidDataSource();
}
@Bean
public ServletRegistrationBean
<StatViewServlet> statViewServlet() {
// Register the Druid monitoring Servlet
ServletRegistrationBean
<StatViewServlet> bean =
new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> params = new HashMap<>();
// Set the monitoring page login username
params.put("loginUsername", "druid");
// Set the monitoring page login password
params.put("loginPassword", "d1234");
// Allow only local access; change this to your office network range if needed
params.put("allow", "localhost");
bean.setInitParameters(params);
return bean;
}
@Bean
public FilterRegistrationBean
<WebStatFilter> webStatFilter() {
FilterRegistrationBean
<WebStatFilter> bean = new FilterRegistrationBean<>();
// Register the Web statistics Filter
bean.setFilter(new WebStatFilter());
Map<String, String> params = new HashMap<>();
// Exclude static assets and the monitoring path to reduce noise in statistics
params.put("exclusions", "*.js,*.css,/druid/*");
bean.setUrlPatterns(Arrays.asList("/*"));
bean.setInitParameters(params);
return bean;
}
}
This code provides fine-grained control over the Druid monitoring page and statistics filtering behavior.
The monitoring page exposes connection pool and SQL execution status directly
After you start the application, open http://localhost:8080/druid/ and sign in with the configured credentials to view connection pool status, SQL execution counts, slow queries, and session information.
AI Visual Insight: This image shows a typical monitoring view of the Druid Web console. It usually includes active datasource connections, pooled connection distribution, SQL execution statistics, request trends, and session information. For developers, this type of dashboard helps quickly identify connection leaks, clusters of slow SQL, and database load peaks.
Production environments require explicit security and performance boundaries
The monitoring page exposes database access statistics by default, so in production you should not rely on a weak password alone. A safer approach is to combine IP allowlists, reverse-proxy authentication, or restrict access to internal networks only.
Monitoring also introduces overhead. In high-concurrency systems, decide whether to enable detailed SQL statistics based on troubleshooting needs instead of recording everything continuously.
@Bean
public ServletRegistrationBean
<StatViewServlet> secureStatViewServlet() {
ServletRegistrationBean
<StatViewServlet> bean =
new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> params = new HashMap<>();
// Apply a stronger access control policy
params.put("allow", "127.0.0.1,10.0.0.0/8");
// Block high-risk sources
params.put("deny", "0.0.0.0/0");
bean.setInitParameters(params);
return bean;
}
This example emphasizes that the monitoring page needs additional access control in production.
This approach fits Java services that need SQL observability and database diagnostics
If your project only handles simple CRUD, the default connection pool may already be sufficient. But if you need to inspect pool usage, analyze slow SQL, and quickly locate database-side performance issues, Druid remains a direct and effective choice.
Combined with Spring Boot’s configuration-driven model, Druid has a low integration cost and delivers high monitoring value. It is especially suitable for internal business systems, admin platforms, and services with noticeable database pressure.
FAQ
Which Druid dependency should I use with Spring Boot 3?
Use com.alibaba:druid-spring-boot-3-starter. It is designed for the Spring Boot 3 ecosystem and avoids compatibility issues that older starters may have under the Jakarta namespace.
Why is the monitoring page unavailable even though Druid is configured?
There are three common causes: stat-view-servlet.enabled is not enabled, the project path or port is incorrect, or the Servlet is blocked by your security framework. Start by checking whether the /druid/ path and configuration properties are effective.
Does Druid monitoring affect production performance?
Yes, it adds some overhead, especially when detailed SQL statistics and Web request collection are enabled. In high-concurrency production environments, enable it only when needed, limit the sampling scope, and expose the monitoring page only to internal networks or operations endpoints.
Core summary
This guide reconstructs the core workflow for integrating Druid with Spring Boot, covering dependency setup, datasource configuration, monitoring page enablement, custom Servlet and Filter registration, and production security recommendations. It helps developers quickly implement database connection pool monitoring and SQL observability.