代码块
以下为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容器。
以上为个人所遇到的错误 为不在踩坑问题记录,如帮助到您还请点个赞