spring boot 整合 druid 配置 记录

1 <dependency> 2   <groupId>com.alibaba</groupId> 3   <artifactId>druid</artifactId> 4   <version>1.1.21</version> 5 </dependency>

 druid坐标

 

spring: datasource: username: root password: 2652779 url: jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

 

druid 配置:

1 @Configuration 2 public class DruidConfig { 3 4 @ConfigurationProperties(prefix = "spring.datasource") 5 @Bean 6 public DataSource druidDataSource() { 7 return new DruidDataSource(); 8 } 9 10 //后台监控 11 @Bean 12 public ServletRegistrationBean statViewServlet() { 13 14 ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); 15 16 //后台需要有人登录的登录密码 17 HashMap<String, String> initParameters = new HashMap<>(); 18 19 //增加配置 20 initParameters.put("loginUsername", "admin"); //登录的key 是固定的 loginUsername loginPassword 21 initParameters.put("loginPassword", "123456"); 22 23 //允许谁可以访问 24 initParameters.put("allow", ""); 25 26 //禁止谁可以访问 27 // initParameters.put("kuangsheng", "192.168.11.123"); 28 29 bean.setInitParameters(initParameters); 30 return bean; 31 } 32 33 //filter 34 @Bean 35 public FilterRegistrationBean webStatFilter() { 36 FilterRegistrationBean bean = new FilterRegistrationBean(); 37 38 bean.setFilter(new WebStatFilter()); 39 40 41 HashMap<String, String> initParment = new HashMap<>(); 42 //过滤请求 43 initParment.put("exclusions", "*.js,*.css,/druid/*"); 44 45 return bean; 46 } 47 48 49 }

 


__EOF__

本文作者无魂儿
本文链接https://www.cnblogs.com/nosouln/p/12686743.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   无魂儿  阅读(311)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示