SpringBoot中注入RedisTemplate实例异常解决

错误信息:

Error creating bean with name 'servlet02': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate

原因 :

@Autowird注解是按照类型注入的,即Spring容器中的bean的class,@Resource是按照Spring容器中的id 名自动注入的

在Springboot这种使用redis,Spring容器中存在两个redis对象:

RedisTmplate 和 StringRedisTemplate

@RestController
@RequestMapping("/user2")
public class servlet02 {

// @Qualifier("redisTemplate")
@Autowired
private RedisTemplate redisTemplate; // 注入redis对象

@Resource
private StringRedisTemplate r1;

@RequestMapping("/id")
public void aa(){
System.out.println(redisTemplate.getClass());
System.out.println("-------");
System.out.println(r1.getClass());

}
}

运行结果:

因为StringRedisTemplate继承了类 : RedisTemplate<String, String>

也就是说,程序在编译完成以后,会消除泛型的指定,最后程序中会出现两个 一样的RedisTemplate对象,

@RestController
@RequestMapping("/user2")
public class servlet02 {

// @Qualifier("redisTemplate")
@Autowired
private RedisTemplate<String,String> redisTemplate0; // 注入redis对象

@Resource
private StringRedisTemplate r1;

@RequestMapping("/id")
public void aa(){
System.out.println(redisTemplate0.getClass());
// System.out.println("-------");
System.out.println(r1.getClass());

}
}

因为@Autowird是按照类型注入的,所以 会出现错误:

Field redisTemplate0 in com.controller.servlet02 required a single bean, but 2 were found:

- redisTemplate: defined by method 'redisTemplate' in class path resource

即存在两个RedisTemplate对象

如果把 @Autowird注解换成@Resource注解,会出现错误:

@Resource
private RedisTemplate redisTemplate0; // 注入redis对象

No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate<?, ?>' available: expected single matching bean but found 2:

解决办法有5种:

1、

@Autowired
private RedisTemplate redisTemplate; // 注入redis对象,必须是这个变量名

2、

@Qualifier("stringRedisTemplate")
@Autowired
private RedisTemplate r1; // 注入redis对象

3、

@Qualifier("redisTemplate")
@Autowired
private RedisTemplate r2;

4、

@Resource
private StringRedisTemplate r3;

5、

@Resource
private RedisTemplate<String,String> r4;

ps : 我只是一名初学者,目前刚自学到Spring boot 阶段,在整合redis的时候遇到了这个问题,并且网上都没有给出有效的解决方案,所以就顺便发出来。因为我刚好在记录这个错误,所以就顺便发出来了

 

以上的结论纯属我个人猜测,虽然可能会有错误的地方,但是能找到解决方法,就说明应该没有错

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Spring Boot 使用 RedisTemplate 存储 hash 类型的数据到 Redis,可以使用以下步骤: 1. 在 pom.xml 文件添加 Redis 的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在 application.properties 或 application.yml 配置 Redis 连接信息: ``` spring.redis.host=your_redis_host spring.redis.port=your_redis_port ``` 3. 在需要使用 Redis 的类注入 RedisTemplate 对象: ``` @Autowired private RedisTemplate<String, Object> redisTemplate; ``` 4. 使用 redisTemplate 的 hash 相关方法来存储数据,如 hset()、hget()、hdel() 等。 示例代码: ``` redisTemplate.opsForHash().put("hashName","key","value"); redisTemplate.opsForHash().get("hashName","key"); ``` 注意: RedisTemplate 默认会使用 JDK 序列化器来序列化对象,如果需要使用 JSON 序列化器,需要在配置类进行配置。 ### 回答2: 在Spring Boot使用RedisTemplate存放hash类型进Redis,我们需要进行以下步骤: 1. 配置RedisTemplate 首先,在Spring Boot的配置文件(例如application.properties)配置Redis的连接信息,包括主机地址、端口号、密码等。可以使用以下配置代码: ```java spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=yourpassword ``` 然后,在你的Spring Boot应用程序创建一个RedisTemplateBean,并设置相关的Redis连接工厂、Key和Value的序列化方式等配置项。 2. 使用RedisTemplate操作hash类型 在你的代码注入RedisTemplate,并使用它对Redis的hash类型进行操作。例如,你可以在某个Service类使用RedisTemplate进行hash的存储和获取操作,示例如下: ```java @Service public class RedisHashService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveHash(String key, Map<String, Object> hash) { redisTemplate.opsForHash().putAll(key, hash); } public Map<Object, Object> getHash(String key) { return redisTemplate.opsForHash().entries(key); } } ``` 在上述示例,`saveHash`方法使用RedisTemplate的`opsForHash`方法来将一个Map对象保存为hash类型的数据到Redis。`getHash`方法则使用`opsForHash`方法获取指定hash的所有字段和值,返回一个Map对象。 这样,你就可以在Spring Boot使用RedisTemplate存放hash类型进Redis了。通过注入RedisTemplate,并使用其提供的操作方法,你可以方便地进行哈希类型的数据存储和获取操作。 ### 回答3: 在Spring Boot使用RedisTemplate存储hash类型数据到Redis,可以按照以下步骤进行操作: 首先,我们需要在Spring Boot项目引入Redis依赖,在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来需要配置Redis连接信息,可以在application.properties文件添加以下配置: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 然后,在需要使用Redis的类注入RedisTemplate实例: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; ``` 接下来就可以使用RedisTemplate存储hash类型数据到Redis了。假设我们要存储一个名为"myHash"的hash数据到Redis,可以使用以下代码: ```java String hashKey = "myHash"; String field1 = "field1"; String value1 = "value1"; String field2 = "field2"; String value2 = "value2"; redisTemplate.opsForHash().put(hashKey, field1, value1); redisTemplate.opsForHash().put(hashKey, field2, value2); ``` 可以使用opsForHash()方法获取HashOperations对象,然后调用put()方法将指定字段和对应值存储到hash。 当然,还可以使用其他的方法进行操作,例如获取hash指定字段的值、获取全部字段和值等等。详细的操作可以查看RedisTemplate和HashOperations类的方法。 最后,记得在应用程序关闭时释放Redis连接资源。 以上就是使用RedisTemplate存储hash类型数据到Redis的简要步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值