官方文档 - Druid Spring Boot Starter
引入依赖**:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
分析自动配置:
- 扩展配置项 spring.datasource.druid。
- 自动配置类DruidDataSourceAutoConfigure。
- DruidSpringAopConfiguration.class, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns。
- DruidStatViewServletConfiguration.class, 监控页的配置。spring.datasource.druid.stat-view-servlet默认开启。
- DruidWebStatFilterConfiguration.class,web监控配置。spring.datasource.druid.web-stat-filter默认开启。
- DruidFilterConfiguration.class所有Druid的filter的配置: ```java private static final String FILTER_STAT_PREFIX = “spring.datasource.druid.filter.stat”; private static final String FILTER_CONFIG_PREFIX = “spring.datasource.druid.filter.config”; private static final String FILTER_ENCODING_PREFIX = “spring.datasource.druid.filter.encoding”; private static final String FILTER_SLF4J_PREFIX = “spring.datasource.druid.filter.slf4j”; private static final String FILTER_LOG4J_PREFIX = “spring.datasource.druid.filter.log4j”; private static final String FILTER_LOG4J2_PREFIX = “spring.datasource.druid.filter.log4j2”; private static final String FILTER_COMMONS_LOG_PREFIX = “spring.datasource.druid.filter.commons-log”; private static final String FILTER_WALL_PREFIX = “spring.datasource.druid.filter.wall”;
**application.yaml中配置。**
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
username: root
password: 941941
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
aop-patterns: com.atguigu.admin.* #监控SpringBean
filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙)
# 配置监控页功能
stat-view-servlet:
enabled: true #是否开启监控页
login-username: wzy #设置用户名
login-password: 941941 #设置密码
resetEnable: false
# 监控web
web-stat-filter:
enabled: true #是否开启web监控
urlPattern: /* #路径下的所有都监控
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' #带这些后缀的除外
# 对上面filters里面的stat的详细配置
filter:
stat:
enabled: true #是否开启sql监控
slow-sql-millis: 1000
logSlowSql: true #是否记录man查询
wall:
enabled: true #是否开启防火墙
config:
update-allow: false #所有的sql修改操作,都会被防火墙拦截。
drop-table-allow: false #所有sql删除表操作,都会被防火墙拦截。