官方文档 - Druid Spring Boot Starter

    引入依赖**:

    1. <dependency>
    2. <groupId>com.alibaba</groupId>
    3. <artifactId>druid-spring-boot-starter</artifactId>
    4. <version>1.1.17</version>
    5. </dependency>

    分析自动配置:

    1. 扩展配置项 spring.datasource.druid。
    2. 自动配置类DruidDataSourceAutoConfigure。
    3. DruidSpringAopConfiguration.class, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns。
    4. DruidStatViewServletConfiguration.class, 监控页的配置。spring.datasource.druid.stat-view-servlet默认开启。
    5. DruidWebStatFilterConfiguration.class,web监控配置。spring.datasource.druid.web-stat-filter默认开启。
    6. 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”;
    1. **application.yaml中配置。**
    2. ```yaml
    3. spring:
    4. datasource:
    5. url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
    6. username: root
    7. password: 941941
    8. driver-class-name: com.mysql.cj.jdbc.Driver
    9. druid:
    10. aop-patterns: com.atguigu.admin.* #监控SpringBean
    11. filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙)
    12. # 配置监控页功能
    13. stat-view-servlet:
    14. enabled: true #是否开启监控页
    15. login-username: wzy #设置用户名
    16. login-password: 941941 #设置密码
    17. resetEnable: false
    18. # 监控web
    19. web-stat-filter:
    20. enabled: true #是否开启web监控
    21. urlPattern: /* #路径下的所有都监控
    22. exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' #带这些后缀的除外
    23. # 对上面filters里面的stat的详细配置
    24. filter:
    25. stat:
    26. enabled: true #是否开启sql监控
    27. slow-sql-millis: 1000
    28. logSlowSql: true #是否记录man查询
    29. wall:
    30. enabled: true #是否开启防火墙
    31. config:
    32. update-allow: false #所有的sql修改操作,都会被防火墙拦截。
    33. drop-table-allow: false #所有sql删除表操作,都会被防火墙拦截。