Databases 15 min read

Master Druid Monitoring: Configure, Filter, and Remove Ads in Spring Boot

This guide explains how to integrate Alibaba Druid as a Java database connection pool, add Maven dependencies, configure core properties, enable SQL and slow‑SQL monitoring, set up WebStatFilter and Spring AOP monitoring, remove the built‑in advertisement, and retrieve monitoring data via DruidStatManagerFacade.

Architect
Architect
Architect
Master Druid Monitoring: Configure, Filter, and Remove Ads in Spring Boot

1. Basic Concepts

Druid is a high‑performance Java database connection pool from Alibaba, offering strong monitoring and extensibility compared with HikariCP.

2. Add 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>

3. Configure Related Properties

Configure Druid datasource – set initial size, max active, min idle, etc., similar to other pools.

Configure WebStatFilter – monitors web requests, SQL execution time, URLs, sessions, table access.

Configure StatViewServlet – provides a web UI for viewing Druid statistics; set login credentials.

Note: All Druid properties follow the Druid naming and can be set in application.yml or application.properties.

4. SQL Monitoring (WebStatFilter)

spring:
  datasource:
    druid:
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 1000

5. Slow SQL Recording

spring:
  datasource:
    druid:
      filter:
        stat:
          enabled: true
          db-type: mysql
          log-slow-sql: true
          slow-sql-millis: 2000

When enabled, SQL statements exceeding the configured threshold are logged.

6. Spring Monitoring (AOP)

<!-- Spring Boot AOP starter -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Configure the AOP pointcut in application.yml:

spring.datasource.druid.aop-patterns="com.springboot.template.dao.*"

7. Remove Advertisement

Two ways to eliminate the footer ad displayed on Druid’s monitoring pages:

Manually comment out the line // this.buildFooter(); in the common.js file located inside the Druid JAR.

Register a custom filter that intercepts the common.js request and strips the ad HTML via regular‑expression replacement.

@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name="spring.datasource.druid.stat-view-servlet.enabled", havingValue="true", matchIfMissing=true)
public class RemoveDruidAdConfig {
    @Bean
    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
        final String filePath = "support/http/resources/js/common.js";
        Filter filter = new Filter() {
            @Override public void init(FilterConfig filterConfig) throws ServletException {}
            @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                chain.doFilter(request, response);
                response.resetBuffer();
                String text = Utils.readFromResource(filePath);
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                response.getWriter().write(text);
            }
            @Override public void destroy() {}
        };
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);
        return registrationBean;
    }
}

8. Retrieve Druid Monitoring Data

After enabling StatFilter, you can obtain datasource statistics via DruidStatManagerFacade:

@RestController
@RequestMapping("/druid")
public class DruidStatController {
    @GetMapping("/stat")
    public Object druidStat() {
        return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
    }
}

The above endpoint returns a list of monitoring data for all configured Druid data sources.

Monitoring Pages (Screenshots)

Login page
Login page
Data source page
Data source page
SQL monitoring page
SQL monitoring page
URL monitoring page
URL monitoring page
Spring monitoring page
Spring monitoring page
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLConnection PoolSpring BootDruidDatabase Monitoring
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.