1. RedisTemplate cannot be injected using @Autowired
1.1 Error code
@RestController
public class StudentController {
@Autowired
private RedisTemplate redis;
@PostMapping("set")
@ResponseBody
public void set(@RequestBody Student student){
redis.opsForValue().set("student",student);
}
1.2 Error message
***************************
APPLICATION FAILED TO START
***************************
Description:
Field redis in com.springboot.demo.controller.StudentController required a single bean, but 2 were found:
- redisTemplate: defined by method 'redisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]
- stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Process finished with exit code 1
2. The reason for the error
After importing Redis dependencies in Spring Boot, SpringBoot will automatically help us generate a RedisTemplate and a StringRedisTemplate in the container, so it will report:
Consider marking one of the beans as @Primary,
updating the consumer to accept multiple beans,
or using @Qualifier to identify the bean that should be consumed