您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 2浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

springboot整合druid报错:Failed to bind properties under 'spring.datasource' to javax.sql.DataSource

小志的博客 发布时间:2019-11-11 11:29:00 ,浏览量:2

一、报错如下:
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只引入了控制台的日志输入,警告消失,如下图: 在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0440s