RedisConfig.java序列化配置
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Created with IDEA
* Author: www.itze.cn
* Date: 2021-05-18
* Email:gitlab@111.com
*/
public class redisConfig {
@Autowired
private RedisTemplate redisTemplate;
/**
* 设置序列化
* key设置String类型序列化
* value设置阿里的FastJson序列化
*
* @return
*/
@Bean
public RedisTemplate redisTemplateInit() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
return redisTemplate;
}
}
重点!!!在applicationContext.xml上下文配置中引入application-redis.xml
<import resource="classpath:redis/application-redis.xml"/>
如果application-redis.xml配置文件中,标注<-- !!!这段代码可以删除!!!-->里面包含的代码和RedisConfig.java同时存在,Spring将会以RedisConfig.java为高优先级使用过,如果配置了RedisConfig.java,记得在application-redis.xml中引入,本文在xml文件中最下面,xml中注释可删除的代码可有可无
针对SpringBoot项目,如果使用的是SpringBoot项目只需要在RedisConfig.java类的上面加上@Configuration 注解即可,无需配置文件,SpringBoot启动时会扫描配置类注解