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

  • Post author:
  • Post category:java

在配置SpringBoot中在application.yml文件中配置了如下代码:

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/appinfodb
    driver-class-name: com.mysql.jdbc.Driver
    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

在测试类Debug时,出现如下错误:

Error creating bean with name 'druid': Could not bind properties to 'DruidDataSource' : prefix=spring.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource' to javax.sql.DataSource

最后查明原因是在application.yml文件中配置了日志:

filters: stat,wall,log4j

而在pom,xml中并没有引入log4j依赖包
最后在pom.xml中加入如下代码

<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
</dependency>

最后测试成功


版权声明:本文为w1316022737原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。