Spring boot注入多个redisTemplate,并更改默认序列化策略

该博客详细介绍了如何在Spring Boot应用中配置多个RedisTemplate,分别针对不同类型的键值对(String, Long, Integer),并展示了如何更改默认序列化策略为Jackson2JsonRedisSerializer。通过创建配置类RedisConfig,定义了不同的RedisTemplate Bean,并在RedisController中进行测试,展示了Set、Hash和String的操作。" 113089970,10543589,Qt实现Onvif网络配置功能详解,"['Qt开发', 'Onvif协议', '视频监控', '网络设置', '设备管理']
摘要由CSDN通过智能技术生成
1、添加maven依赖
        <dependency>
		   <groupId>org.springframework.boot</groupId>
		   <artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
2、配置文件中增加redis连接相关信息
spring.redis.hostName=192.168.0.134
spring.redis.port=6379
spring.redis.database=0
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-2
3、创建一个配置类
package com.example.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableAutoConfiguration
public class RedisConfig {

    private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Bean
    @ConfigurationProperties(prefix="spring.redis")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix="spring.redis")
    public JedisConnectionFactory getConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        logger.info("JedisConnectionFactory bean init success."
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot中,可以使用RedisTemplate来操作Redis数据库。当需要将LocalDateTime类型的数据存储到Redis中时,需要对其进行序列化和反序列化处理。 首先,需要配置RedisTemplate序列化方式为Jackson2JsonRedisSerializer。在配置类中添加以下代码: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 设置key的序列化方式为StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); // 设置value的序列化方式为Jackson2JsonRedisSerializer template.setValueSerializer(new Jackson2JsonRedisSerializer<>(LocalDateTime.class)); return template; } } ``` 然后,在需要使用RedisTemplate的地方,可以直接注入RedisTemplate,并使用其提供的方法进行操作。例如,将LocalDateTime类型的数据存储到Redis中: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveLocalDateTime(String key, LocalDateTime value) { redisTemplate.opsForValue().set(key, value); } ``` 同样地,可以使用RedisTemplate获取存储在Redis中的LocalDateTime类型的数据: ```java public LocalDateTime getLocalDateTime(String key) { return (LocalDateTime) redisTemplate.opsForValue().get(key); } ``` 需要注意的是,由于RedisTemplate默认使用JdkSerializationRedisSerializer进行序列化,所以需要自定义序列化方式为Jackson2JsonRedisSerializer,并指定序列化的类型为LocalDateTime。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值