druid官方文档
导入jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring.version}</version>
</dependency>
配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置连接池
druid:
aop-patterns: com.wdz.boot.* # 监控包路径下的bean
filters: stat,wall,slf4 # 开启的功能:stat:sql监控,wall:防火墙,日志
stat-view-servlet: # 配置架空页面功能
enabled: true # 开启页面监控
login-username: admin
login-password: 123456
web-stat-filter: # web监控、uri功能
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
访问:http://localhost:8080/druid
这个配置默认是关闭的,在yml中开启
分析
由于jar包是 spring-boot-starter
所以springboot给druid有自动配置功能
找到druid的依赖
数据源主要配置类:DataSourceAutoConfiguration
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.spring.boot.autoconfigure;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration;
import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidSpringAopConfiguration;
import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidStatViewServletConfiguration;
import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidWebStatFilterConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author lihengming [89921218@qq.com]
*/
@Configuration
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class,
DruidStatViewServletConfiguration.class,
DruidWebStatFilterConfiguration.class,
DruidFilterConfiguration.class})
public class DruidDataSourceAutoConfigure
注解说明:
1、 @ConditionalOnClass(DruidDataSource.class)
当系统中存在DruidDataSource类的时候才会进行初始化配置,当引入druid的时候已经存在这个类,所以这个类自动配置
2、@AutoConfigureBefore(DataSourceAutoConfiguration.class)
在初始化化当前类之前加载DataSourceAutoConfiguration,在springboot官方的数据源加载之前加载
@Bean(initMethod = "init")
@ConditionalOnMissingBean // 容器中不存在当前bean的时候加载
public DataSource dataSource() {
LOGGER.info("Init DruidDataSource");
return new DruidDataSourceWrapper();
}
由于在官方之前加载数据源,所以spring容器中不存在DataSource数据源,然后加载druid的DataSource,以这个为主
3、 @EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
将 DruidStatProperties和DataSourceProperties 加入到容器中
4、 @Import({DruidSpringAopConfiguration.class,DruidStatViewServletConfiguration.class,DruidWebStatFilterConfiguration.class,DruidFilterConfiguration.class})
将druid监控页面用的其他配置加载进来
4.1: DruidSpringAopConfiguration是用来配置页面spring监控的
所有属性配置根是:
spring.datasource.druid.aop-patterns
4.2: DruidStatViewServletConfiguration
主要是配置druid的监控页面:默认是开启的,配置用户名密码访问就是这个类
4.3: DruidWebStatFilterConfiguration,默认是开启的
web监控配置:路径拦截和派出拦截:如默认排除"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
4.4: DruidFilterConfiguration 其他配置的拦截
,如:防火墙、日志等,配置项如下图
由于数据源的配置使用的是DataSourceProperties,所以数据源配置是以spring.datasource开头的配置项
DruidDataSourceWrapper 包装器类,这个类将Spring的DataSource的数据处理到DruidDataSource中