【Redis】タイプ「org.springframework.data.redis.core.RedisTemplate」のBeanが見つかりませんでした

Field redisTemplate in com.lzp.controller.test.RedisController required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.

エラーコード

@ApiIgnore
@RestController
@RequestMapping("redis")
public class RedisController {
    
    

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/get")
    public Object get(String key) {
    
    
        return redisTemplate.opsForValue().get(key);
    }
}

解決策:RedisTemplateの一般的な指定を削除します。

No qualifying bean of type ‘org.springframework.data.redis.core.RedisTemplate< java.lang.String, java.lang.Object>。

公式ウェブサイト、Redisへの接続のセクションにメモがあります

If you add your own @Bean of any of the auto-configured types, it replaces the default (except in the case of RedisTemplate, when the exclusion is based on the bean name, redisTemplate, not its type). 

上記のRedisTemplate <String、Object>は、注入時に@Autowiredを使用し、@ Autowiredはデフォルトでタイプごとにアセンブルされます。つまり、RedisTemplate <String、Object>のBeanを取得する場合は、名前に従ってアセンブルする必要があります。次に、デフォルトで名前でアセンブルされる@Resourceの使用を自然に考えます。したがって、次のコードを変更して、アプリケーションを正常に起動します。

@Resource
private RedisTemplate<String, Object> redisTemplate;

次に、StringRedisTemplateの親クラスはRedisTemplate <String、String>であり、Beanはデフォルトでシングルトンであり、2つは当然同じオブジェクトです。したがって、以下の注入方法も使用できます。

@Autowired
private RedisTemplate<String, String> redisTemplate;

参照リンク:https://blog.csdn.net/zhaoheng314/article/details/81564166

おすすめ

転載: blog.csdn.net/LIZHONGPING00/article/details/115314418