记一次SpringBoot操作redis报错 Error creating bean with name ‘dataSource‘ defined in class path resource解决方法

代码块

以下为Springboot操作Redis一些简单的测试代码

maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

yml配置

spring:
  redis:
    host: 127.0.0.1 #redis服务地址
    port: 6379 #端口
    password:   #用户名:密码

测试类代码

@SpringBootTest
// @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

class LastApplicationTests<SpringRedisTemplate> {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Test
    void redisTest () {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        //向redis中添加一个kv值
        operations.set("hello","world");
        //获得hello键的值
        String hello = operations.get("hello");
        System.out.println(hello);
    }
}

看似非常简单没有错误的一个代码块,踩了很大的坑
在使用springboot操作redis进行一个简单的添加k v值时,遇到了一个坑,解决了很久,起初以为是springboot导入依赖版本的问题,切换了版本还不见问题解决。
在这里插入图片描述
具体报错详情↓

Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDataSourceBeanCreationException: Failed to determine a suitable driver class

错误详情: 创建名为“dataSource”的bean时,在类路径resource中定义了错误。通过工厂方法实例化Bean失败;嵌套异常是org.springframework.beans。BeanInstantiationException:无法实例化[com.zaxxer.hikari.]HikariDataSource:工厂方法’dataSource’抛出异常

解决方法

在测试启动项加上注解 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

在这里插入图片描述

@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。

以上为个人所遇到的错误 为不在踩坑问题记录,如帮助到您还请点个赞

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值