相关推荐recommended
SpringBoot3集成阿里数据库连接池Druid
作者:mmseoamin日期:2024-02-04

1.引入Pom依赖

springBoot版本为当前最新版本 3.1.5



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        3.1.5
         
    
    com.example
    myDreams
    0.0.1-SNAPSHOT
    myDreams
    myDreams
    
        17
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
        
        
            com.alibaba
            druid-spring-boot-3-starter
            1.2.18
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
            8.0.28
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2 修改配置文件

编辑 application.yml文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      url: jdbc:mysql://122.222.141.45:3306/zx
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 10
      max-active: 100
      max-wait: 60000
      min-idle: 10
      test-while-idle: true
      min-evictable-idle-time-millis: 300000
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*   #druid监控页面地址
        reset-enable: false     
        login-username: druid
        login-password: druid

3 避坑,启动解决Druid访问报404问题

resource下创建
META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports
添加com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

SpringBoot3集成阿里数据库连接池Druid,第1张

SpringBoot3集成阿里数据库连接池Druid,第2张

4 编写配置类

@SpringBootApplication
public class MyDreamsApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyDreamsApplication.class, args);
    }
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
}

5 启动项目

本机访问durid地址

启动发现有广告,去掉广告

6 去除druid广告栏

package com.example.mydreams.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot3.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import jakarta.servlet.*;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.sql.DataSource;
import java.io.IOException;
/**
 * @author dgz
 */
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
    @Bean
    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        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 filter = new Filter() {
            @Override
            public void init(FilterConfig filterConfig){
            }
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                chain.doFilter(request, response);
                // 重置缓冲区,响应头不会被重置
                response.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);
                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("
", ""); text = text.replaceAll("powered.*?shrek.wang", ""); response.getWriter().write(text); } @Override public void destroy() { } }; FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(filter); registrationBean.addUrlPatterns(commonJsPattern); return registrationBean; } }

重新启动项目,发现广告被去除,成功,干杯!!!