springboot自定义的redisTemplate为什么会取代系统自带的

springboot里的一个叫@ConditionalOnMissingBean注解,redisTemplate的配置类有这个注解,自定义一个redisTemplate配置类也可以加这个注解。为什么自定义的redisTemplate一定会把springboot自带的redisTemplate取代,而不是springboot自带的把自定义的取代?

springboot自定义的redisTemplate提供了更高级的功能,比如支持序列化和反序列化,支持自定义缓存策略,支持缓存组件,支持灵活的缓存管理等,这些功能都是系统自带的redisTemplate所不具备的,所以可以取代系统自带的redisTemplate。

覆盖啊。你看一下优先级加载

  • 文章:SpringBoot的RedisTemplate配置类 中也许有你想要的答案,请看下吧
  • 除此之外, 这篇博客: Springboot RedisTemplate 分布式锁中的 RedisTemplate.execute 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • execute 有很多参数
    常用的有execute(RedisCallback)execute(SessionCallback)
    通常都是使用SessionCallback,因为封装的更好用,不需要自己转byte
    execute中,你可以在回调中随意获取值,这和executePipelined有很大区别。

    看看两者的区别

           redisTemplate.execute(new SessionCallback<Object>() {
                @Override
                public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {
                      operations.watch("监控key");
                      if ("蛇皮".equals(operations.opsForValue().get("监控key"))){
                          operations.multi();
                          operations.delete("监控key");
                          List<Object> exec = operations.exec();
                          //这里就可以看到结果
                          System.out.println(exec);
                          return exec;
                      }
                      operations.unwatch();
                      return null;
                }
            });