0


SpringBoot3集成阿里数据库连接池Druid

1.引入Pom依赖

springBoot版本为当前最新版本 3.1.5

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>myDreams</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>myDreams</name>
  15. <description>myDreams</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <!-- Druid 连接池引入 -->
  25. <!-- SpringBoot3 -->
  26. <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-3-starter -->
  27. <dependency>
  28. <groupId>com.alibaba</groupId>
  29. <artifactId>druid-spring-boot-3-starter</artifactId>
  30. <version>1.2.18</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-log4j2</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-jdbc</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>mysql</groupId>
  47. <artifactId>mysql-connector-java</artifactId>
  48. <version>8.0.28</version>
  49. </dependency>
  50. </dependencies>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

2 修改配置文件

编辑 application.yml文件

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. druid:
  5. url: jdbc:mysql://122.222.141.45:3306/zx
  6. username: root
  7. password: 123456
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. initial-size: 10
  10. max-active: 100
  11. max-wait: 60000
  12. min-idle: 10
  13. test-while-idle: true
  14. min-evictable-idle-time-millis: 300000
  15. stat-view-servlet:
  16. enabled: true
  17. url-pattern: /druid/* #druid监控页面地址
  18. reset-enable: false
  19. login-username: druid
  20. login-password: druid

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

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

4 编写配置类

  1. @SpringBootApplication
  2. public class MyDreamsApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(MyDreamsApplication.class, args);
  5. }
  6. @ConfigurationProperties(prefix = "spring.datasource.druid")
  7. @Bean
  8. public DataSource druidDataSource() {
  9. return new DruidDataSource();
  10. }
  11. }

5 启动项目

本机访问durid地址

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

6 去除druid广告栏

  1. package com.example.mydreams.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure;
  4. import com.alibaba.druid.spring.boot3.autoconfigure.properties.DruidStatProperties;
  5. import com.alibaba.druid.util.Utils;
  6. import jakarta.servlet.*;
  7. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  8. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  9. import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
  10. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.boot.context.properties.ConfigurationProperties;
  14. import javax.sql.DataSource;
  15. import java.io.IOException;
  16. /**
  17. * @author dgz
  18. */
  19. @Configuration
  20. @ConditionalOnWebApplication
  21. @AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
  22. @ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
  23. public class DruidConfig {
  24. @ConfigurationProperties(prefix = "spring.datasource.druid")
  25. @Bean
  26. public DataSource druidDataSource() {
  27. return new DruidDataSource();
  28. }
  29. @Bean
  30. public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
  31. // 获取web监控页面的参数
  32. DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
  33. // 提取common.js的配置路径
  34. String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
  35. String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
  36. final String filePath = "support/http/resources/js/common.js";
  37. //创建filter进行过滤
  38. Filter filter = new Filter() {
  39. @Override
  40. public void init(FilterConfig filterConfig){
  41. }
  42. @Override
  43. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  44. chain.doFilter(request, response);
  45. // 重置缓冲区,响应头不会被重置
  46. response.resetBuffer();
  47. // 获取common.js
  48. String text = Utils.readFromResource(filePath);
  49. // 正则替换banner, 除去底部的广告信息
  50. text = text.replaceAll("<a.*?banner\"></a><br/>", "");
  51. text = text.replaceAll("powered.*?shrek.wang</a>", "");
  52. response.getWriter().write(text);
  53. }
  54. @Override
  55. public void destroy() {
  56. }
  57. };
  58. FilterRegistrationBean registrationBean = new FilterRegistrationBean();
  59. registrationBean.setFilter(filter);
  60. registrationBean.addUrlPatterns(commonJsPattern);
  61. return registrationBean;
  62. }
  63. }

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


本文转载自: https://blog.csdn.net/duguozhu123/article/details/134233510
版权归原作者 码农佩奇 所有, 如有侵权,请联系我们删除。

“SpringBoot3集成阿里数据库连接池Druid”的评论:

还没有评论