一、报错如下:
Description:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Property: spring.datasource.filters
Value: stat,wall,log4j
Origin: class path resource [application.yml]:22:14
Reason: org.apache.log4j.Priority
Action:
Update your application's configuration
1、springboot整合druid的配置代码如下:
spring:
datasource:
# 数据源基本配置
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/rf_xt
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
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,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
2、相关的配置类代码如下:
package com.rf.springbooot.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
}
1、根据报错信息,配置文件pom.xml文件中缺少log4j配置,引入log4j配置即可,访问maven仓库地址,搜索log4j,如下图:
2、重启启动测试类,报错消失,druid的数据源其他配置属性已绑定到了druid数据源中,如下图:
3、上图中的log4j:WARN No appenders could be found for logger (druid.sql.Connection).警告提示是因为缺少log4j的配置文件,在src/main/resources下加上log4j.properties文件即可。由于是测试,lz只引入了控制台的日志输入,警告消失,如下图: