栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Failed to bind properties under ‘spring.datasource‘ to javax.sql.DataSource:

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Failed to bind properties under ‘spring.datasource‘ to javax.sql.DataSource:

问题:
springboot整合druid时,引入了druid的数据源,导入了log4j依赖报错
Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource:

1、application.yaml 配置文件

spring:
  datasource:
    username: root
    password: 123456
    #假如时区报错了,就增加一个时区的配置就可以了serverTimezone-UTC
    url: jdbc:mysql://localhost:3306/blog?serverTimezone-UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    #自定义数据源
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    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,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

相关配置 DruidConfig

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }


//后台监控
@Bean
public ServletRegistrationBean statViewServlet(){
    ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
    //后台需要有人登陆
    HashMap initParameters = new HashMap<>();

    //增加配置
    initParameters.put("loginUsername","admin");//登陆的KEY是固定的 loginUsername  loginPassword
    initParameters.put("loginPassword","123456");

    //允许谁可访问
    initParameters.put("allow","");

    //禁止谁可以访问
    initParameters.put("jia","192.168.11.23");

    bean.setInitParameters(initParameters);//初始化参数
    return bean;
   }


}

也导入了log4j的依赖

 
        
            org.apache.logging.log4j
            log4j
            2.14.1
        

解决办法:
重新导入log4j的依赖后重启项目

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686923.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号