SpringBoot自定义RedisConig时RedisConnectionFactory显示Could not autowire. No beans of ‘RedisConnectionFact

SpringBoot自定义RedisConig时RedisConnectionFactory显示Could not autowire. No beans of 'RedisConnectionFactory' type found.问题。

使用时如下引用:

@Autowired
private RedisTemplate<String, PatientVo> redisTemplate;
//操作
redisTemplate.opsForList().rightPush(roomCode, vo); //redisTemplate null

redisTemplate一直报null

断点追踪,发现飘红的地方,正常入注了,与飘红无关。报空似乎是@Autowired入注方式没成功。修改为如下成功。

private final RedisTemplate<String, PatientVo> redisTemplate;
    @Autowired
    public RedisQueueUtil(@Qualifier("patientVoRedisTemplate") RedisTemplate<String, PatientVo> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

把排队完整代码记录如下:

SpringBoot配置文件中Redis的配置略。

1、先定义一个VO

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PatientVo implements Serializable {
    private String patientName;
    private String patientCardNo;
    private Integer queueNum;
    private String windowNo;
    private String ampm;
}

2、自定义Redis配置文件

@Configuration
@Slf4j
public class JhRedisConfig {
    /**
     * 创建并配置一个专门用于存储和操作PatientVo对象的RedisTemplate bean。
     *
     * @param factory RedisConnectionFactory对象,用于创建与Redis服务器的连接。
     * @return 配置好的RedisTemplate对象,可用于操作Redis中的PatientVo数据。
     */
    @Bean(name = "patientVoRedisTemplate")
    public RedisTemplate<String, PatientVo> patientVoRedisTemplate(RedisConnectionFactory factory) {
        log.info("Redis配置文件中bean已经加载!");
        RedisTemplate<String, PatientVo> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 使用StringRedisSerializer对键进行序列化,保证键的一致性
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        // 使用Jackson2JsonRedisSerializer对值进行序列化,支持复杂的对象存储
        Jackson2JsonRedisSerializer<PatientVo> redisSerializer = new Jackson2JsonRedisSerializer<>(PatientVo.class);

        // 配置ObjectMapper以处理null值的序列化,提高与PatientVo对象的兼容性
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
        objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL ); // 包含非null值
        redisSerializer.setObjectMapper(objectMapper);

        template.setValueSerializer(redisSerializer);
        template.setHashValueSerializer(redisSerializer);

        try {
            template.afterPropertiesSet(); // 初始化RedisTemplate
        } catch (Exception e) {
            log.error("初始化RedisTemplate失败: ", e);
            // 根据实际情况处理异常,例如回滚操作或其他恢复措施
        }
        return template;
    }
}

3、写一个Redis工具类:

@Slf4j
@Service
public class RedisQueueUtil {
    private final RedisTemplate<String, PatientVo> redisTemplate;
    @Autowired
    public RedisQueueUtil(@Qualifier("patientVoRedisTemplate") RedisTemplate<String, PatientVo> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    /**
     * 将患者信息添加到指定房间号的队列中。
     * 该操作将在Redis中对应的房间号列表的末尾添加一个患者对象。
     *
     * @param roomCode 房间号,作为Redis列表的键。
     * @param vo 患者信息对象,将被添加到列表中。
     */
    public void addPatient2Queue(String roomCode, PatientVo vo) {
        // 使用Redis List的rightPush方法,将患者对象添加到指定房间号的队列末尾
        redisTemplate.opsForList().rightPush(roomCode, vo);
    }
    /**
     * 队长度
     * @param roomCode 诊室编码
     * @return Long长度
     */
    public  Long getQueueLength(String roomCode) {
        return redisTemplate.opsForList().size(roomCode);
    }

    // 获取队列数据
    public List<PatientVo> getQueueData(String roomCode) {
        return redisTemplate.opsForList().range(roomCode, 0, -1);
    }

    // 某人中途离开队伍
    public  void leaveQueue(String roomCode, PatientVo patientVo) {
        redisTemplate.opsForList().remove(roomCode, 0, patientVo);
    }

    // 队首离队
    public  PatientVo headLeaveQueue(String roomCode) {
        PatientVo leftPop = redisTemplate.opsForList().leftPop(roomCode);
        return leftPop;
    }

    // 得到某人队列中的位置
    public List<Integer> getOnesPosition(String roomCode, PatientVo patientVo) {
        List<PatientVo> queueData = getQueueData(roomCode);
        int myPositionBeforeNum = queueData.indexOf(patientVo);
        int myPosition = myPositionBeforeNum + 1;
        int size = queueData.size();
        // 当前排队res[0]人,您排在第res[1]位,前面还有res[2]位。
        List<Integer> result = new ArrayList<>();
        result.add(size);
        result.add(myPosition);
        result.add(myPositionBeforeNum);
        return result;
    }

    // 插队操作
    public  void jumpAQueue(String roomCode, PatientVo jumpChecker, PatientVo targetChecker, Integer jumpType) {
        // 在target前面插队
        if (jumpType.equals(1)) {
            redisTemplate.opsForList().leftPush(roomCode, targetChecker, jumpChecker);
        }
        if (jumpType.equals(2)) {
            redisTemplate.opsForList().rightPush(roomCode, targetChecker, jumpChecker);
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述中提到了在创建redisTemplate出现了错误,错误信息是"Could not autowire. No beans of 'RedisConnectionFactory' type found." \[1\] 这个错误通常是由于Spring Boot版本的问题引起的。根据资料,大部分2.x版本的Spring Boot都会出现这个问题,但并不影响正常使用\[2\]。 解决这个问题的方法是在配置类中手动创建RedisConnectionFactory的bean,并将其注入到redisTemplate中。可以参考下面的代码示例: ```java @Bean public RedisConnectionFactory redisConnectionFactory() { // 在这里配置Redis连接工厂的相关信息 // 例如使用Lettuce连接池: RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName("localhost"); configuration.setPort(6379); LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration); return factory; } @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } ``` 通过手动创建RedisConnectionFactory的bean,并将其注入到redisTemplate中,可以解决"Could not autowire. No beans of 'RedisConnectionFactory' type found."的问题\[3\]。 #### 引用[.reference_title] - *1* [Could not autowire. No beans of ‘RedisConnectionFactorytype found.已解决](https://blog.csdn.net/baiqi123456/article/details/128318413)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring Boot 整合redis:Could not autowire. No beans of ‘RedisConnectionFactorytype found.](https://blog.csdn.net/qq_32923975/article/details/130484863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Could not autowire. No beans of ‘RedisConnectionFactorytype found.](https://blog.csdn.net/qq_67972273/article/details/126388082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值