Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何在Spring引导应用程序中创建第二个RedisTemplate实例_Spring Boot_Spring Data Redis - Fatal编程技术网

Spring boot 如何在Spring引导应用程序中创建第二个RedisTemplate实例

Spring boot 如何在Spring引导应用程序中创建第二个RedisTemplate实例,spring-boot,spring-data-redis,Spring Boot,Spring Data Redis,根据,一个RedisTemplate不能支持多个值序列化程序。因此,我想为不同的需要创建多个RedisCacheManager模板,特别是一个用于字符串操作,一个用于对象到JSON序列化,以便在RedisCacheManager中使用。我使用的是Spring Boot,当前的RedisTemplate是自动连接的,我想知道如何正确地声明第二个RedisTemplate实例共享同一个Jedis连接工厂,但有自己的序列化程序 在两个不同的组件中尝试了类似的东西 第1部分声明 @Autowir

根据,一个
RedisTemplate
不能支持多个值序列化程序。因此,我想为不同的需要创建多个RedisCacheManager模板,特别是一个用于字符串操作,一个用于对象到JSON序列化,以便在
RedisCacheManager
中使用。我使用的是Spring Boot,当前的
RedisTemplate
是自动连接的,我想知道如何正确地声明第二个
RedisTemplate
实例共享同一个Jedis连接工厂,但有自己的序列化程序

在两个不同的组件中尝试了类似的东西

第1部分声明

    @Autowired
    private RedisTemplate redisTemplate;

    redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Instance.class));
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
第2部分声明

    @Autowired
    private RedisTemplate redisTemplate;

    redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Instance.class));
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
在这种情况下,两个模板实际上是相同的。跟踪到Spring代码,发现组件1的模板已解析为自动配置
stringRedisTemplate

手动调用RedisTemplate的构造函数,然后调用它的afterPropertiesSet()也无法工作,因为它抱怨找不到连接工厂


我知道这个请求可能与在Spring应用程序中定义另一个bean没有太大区别,但我不确定在当前的SpringDataRedis集成中,我最好的方式是什么。请提供帮助,谢谢。

您可以按照两种方法在一个Spring Boot应用程序中使用多个
RedisTemplate
s:

  • 使用
    @Autowired@Qualifier(“beanname”)重新创建myTemplate
    的命名bean注入,并使用
    @bean(name=“beanname”)
    创建bean
  • 通过在
    RedisTemplate
    上指定类型参数来键入安全注入(例如
    @autowiredredistemplate byteTemplate
    @autowiredistemplate stringTemplate
  • 下面是创建两个不同的应用程序的代码:

    @Configuration
    public class Config {
    
        @Bean
        public RedisTemplate<String, String> stringTemplate(RedisConnectionFactory redisConnectionFactory) {
    
            RedisTemplate<String, String> stringTemplate = new RedisTemplate<>();
            stringTemplate.setConnectionFactory(redisConnectionFactory);
            stringTemplate.setDefaultSerializer(new StringRedisSerializer());
            stringTemplate.afterPropertiesSet();
    
            return stringTemplate;
        }
    
        @Bean
        public RedisTemplate<byte[], byte[]> byteTemplate(RedisConnectionFactory redisConnectionFactory) {
    
            RedisTemplate<byte[], byte[]> byteTemplate = new RedisTemplate<>();
            byteTemplate.setConnectionFactory(redisConnectionFactory);
            byteTemplate.afterPropertiesSet();
    
            return byteTemplate;
        }
    
    }
    
    @配置
    公共类配置{
    @豆子
    公共RedisTemplate stringTemplate(RedisConnectionFactory RedisConnectionFactory){
    RedisTemplate stringTemplate=新RedisTemplate();
    stringTemplate.setConnectionFactory(RedConnectionFactory);
    setDefaultSerializer(新的StringRedisSerializer());
    stringTemplate.AfterPropertieSet();
    返回字符串模板;
    }
    @豆子
    公共RedisTemplate byteTemplate(RedisConnectionFactory RedisConnectionFactory){
    RedisTemplate byteTemplate=新RedisTemplate();
    byteTemplate.setConnectionFactory(重新连接工厂);
    byteTemplate.afterPropertiesSet();
    返回模板;
    }
    }
    

    嗯,马克

    非常感谢,这解决了我的问题。接下来的一个问题是,当我声明这样一个bean时,Spring如何知道为我传递所需的RedisConnectionFactory?这可能是因为我仍然对弹簧机构很熟悉,如果你能教我这方面的知识,我将不胜感激。当只有一个连接工厂时,不用担心。如果有多个,则必须再次对其进行限定,否则最终将出现
    nouniquebeandfinitionexception