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

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

druid配置文件

package com.an.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {


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


    //后台监控,相当于servlet,web.xml
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean= new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //后台账号配置
        HashMap<String,String> initParameters =  new HashMap<>();
        //登录的key使固定的loginUsername,loginPassword
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");
        //允许谁可以访问
        initParameters.put("allow","");
        servletRegistrationBean.setInitParameters(initParameters);
        return servletRegistrationBean;

    }

}

application.yml

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mybatis_datasource?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource


    #SpringBoot默认是不注入这些的,需要自己绑定
    #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.Properity
    #则导入log4j 依赖就行
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

出现的错误

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]:21:14
    Reason: Unable to set value for property filters
 

解决方法

		<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>compile</scope>
        </dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误消息表明,在 spring.datasource.type 下的属性绑定失败,无法将其绑定到 java.lang.class<javax.sql.datasource>。这可能是由于配置错误导致的,请检查配置文件并确保所有属性都是正确的。 ### 回答2: 该错误信息表示在Spring Boot应用程序中,出现了无法将spring.datasource.type属性绑定到javax.sql.DataSource类类型的错误。这通常是由于以下几个原因导致的: 1. Spring Boot版本不匹配:请确保在应用程序中使用的Spring Boot版本与引用的依赖项版本相匹配。如果两者不匹配可能会导致配置错误。 2. 数据库驱动程序库缺失:请确保应用程序中包含正确版本的数据库驱动程序库。如果缺少驱动程序库可能会导致无法找到或识别正确的数据源。 3. 配置错误:请检查应用程序中的配置文件,确保所有的属性值都正确设置。特别是spring.datasource.type属性必须设置为javax.sql.DataSource才能正确绑定。如果属性值不正确可能会导致无法正确绑定。 4. 应用程序正在运行:请确保应用程序没有在运行中,否则可能会使用已关联的端口和进程ID,导致无法bind新的属性。 针对这些原因,可以尝试以下解决方法: 1. 更新Spring Boot版本:请使用与依赖项版本相匹配的Spring Boot版本,以确保兼容性。 2. 安装数据库驱动程序库:请确保应用程序中已安装正确版本的数据库驱动程序库,并且驱动程序库正确设置在由spring.datasource.driver-class-name指定的属性中。 3. 检查配置文件:请检查应用程序中的配置文件并确保所有属性设置正确。特别是必须设置的spring.datasource.type属性。 4. 停止应用程序:请确保应用程序没有在运行中,在停止应用程序之后,再重新启动应用程序。 总之,以上这些解决方案可能会帮助你解决这个错误。但是请注意,解决此错误需要深入了解应用程序的具体情况,不同的应用程序有不同的解决方案。如果你仍然无法解决它,可以参考一下Spring Boot官方文档或向社区寻求帮助。 ### 回答3: 你遇到的错误是由于在Spring Boot项目中没有正确配置数据源所导致的。在Spring Boot中,数据源是一种必要的组件,它负责管理应用程序的连接池和数据库交互。当您尝试将数据源设置为类型为javax.sql.DataSource的类时,系统无法找到此类,因为它不存在于您的类路径中。 为了解决这个问题,您需要检查以下几个方面: 1. 检查您的pom.xml文件或gradle.build文件,确保您已正确添加了数据库驱动程序。驱动程序的Jar文件应该在类路径中。 2. 确保您的application.properties或application.yml中有正确的数据源属性。首先请确定是否使用了以下示例属性: spring.datasource.type=com.zaxxer.hikari.HikariDataSource 如果使用其他数据源类型,例如org.apache.tomcat.jdbc.pool.DataSource,可以在此引用其他数据源; 3. 确保您的配置文件没有任何拼写错误或类型错误。 如果您已经检查了上述所有内容,但仍然看到此错误,请尝试清除Maven或Gradle缓存并重新构建项目。在建立项目之前,建议您查看Spring Boot文档,了解如何正确地为您的应用程序配置数据源。在开发过程中,遵循最佳实践可确保应用程序稳定可靠地运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值