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

Failed to bind properties under ‘spring.datasource‘ to javax.sql.DataSource:,第1张

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的依赖后重启项目

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5687446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存