记一次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容器。

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这个错误通常是由于在Spring应用中使用异步注解时出现的问题。可能是因为缺少必要的配置或依赖项。 首先,请确保您的项目中已正确配置了Redis相关的依赖项。您可以通过在您的pom.xml(如果是Maven项目)或build.gradle(如果是Gradle项目)文件中添加以下依赖项来引入Redis相关的库: 对于Maven项目: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 对于Gradle项目: ```groovy implementation 'org.springframework.boot:spring-boot-starter-data-redis' ``` 如果您已经正确添加了这些依赖项,但仍然遇到此错误,请尝试以下解决方法: 1. 确保您的Spring配置类上使用了`@EnableAsync`注解,以启用异步支持。 2. 检查您的ApplicationContext是否正确加载了Spring的异步处理器。您可以在您的Spring配置类中添加以下代码: ```java @Bean public AsyncTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); return executor; } ``` 3. 如果您使用的是Spring Boot,尝试重启应用程序并清除任何可能导致冲突的缓存或临时文件。 4. 确保您的Redis配置正确,并且您可以成功连接到Redis服务器。 如果您仍然遇到问题,请提供更多关于您的项目配置和错误日志的详细信息,以便我能够更好地帮助您。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值