com.admin4j.redis.RedisAutoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redis-spring-boot-starter Show documentation
Show all versions of redis-spring-boot-starter Show documentation
redis序列化器自动装配starter。默认使用jackson,支持fastjson,fastjson2
package com.admin4j.redis;
import com.admin4j.redis.factory.RedisSerializerFactory;
import com.admin4j.redis.fastjson.FastJson22JsonRedisSerializer;
import com.admin4j.redis.fastjson.FastJson2JsonRedisSerializer;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
* @author andanyang
* @since 2023/5/15 13:35
*/
@Configuration
@AutoConfigureBefore(org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class)
public class RedisAutoConfiguration {
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean("redisTemplate")
@ConditionalOnClass(name = {"com.admin4j.redis.fastjson.FastJson2JsonRedisSerializer"})
public RedisTemplate fastJSONRedisTemplate(RedisConnectionFactory connectionFactory) {
return (RedisTemplate) RedisSerializerFactory.serializer(connectionFactory, new FastJson2JsonRedisSerializer<>(Object.class));
}
@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean("redisTemplate")
@ConditionalOnClass(name = {"com.admin4j.redis.fastjson.FastJson22JsonRedisSerializer"})
public RedisTemplate fastJSON2RedisTemplate(RedisConnectionFactory connectionFactory) {
return (RedisTemplate) RedisSerializerFactory.serializer(connectionFactory, new FastJson22JsonRedisSerializer<>(Object.class));
}
@Bean("redisTemplate")
@Order(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
return RedisSerializerFactory.defaultRedisSerializer(connectionFactory);
}
@Bean
public RedisService redisService(RedisTemplate redisTemplate) {
return new RedisService(redisTemplate);
}
/**
* 默认的 序列化
*
* @return
*/
//
//@Bean("redisTemplate")
//@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
//@Order(2)
//public RedisTemplate jacksonRedisTemplate(RedisConnectionFactory connectionFactory) {
//
// //jackson序列化工具
// Jackson2JsonRedisSerializer