redis configuration class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * Redis configuration class 
 * @program: springbootdemo 
 * @date: 2019/2/22 15:20 
 * @author: zjjlive 
 * @Description: 
 * / 
@Configuration 
@EnableCaching // open annotation 
public class RedisConfig the extends CachingConfigurerSupport { 

    / ** 
     * Related retemplate 
     * @param Factory 
     * @return 
     * / 
    @Bean 
    public RedisTemplate <String, Object> redisTemplate (RedisConnectionFactory Factory) { 

        RedisTemplate <String, Object> = new new RedisTemplate Template <> (); 
        // 配置连接工厂
        template.setConnectionFactory (Factory); 

        / / value Jackson2JsonRedisSerializer value used to serialize and deserialize the redis (JDK use default serialization)
        = New new Jackson2JsonRedisSerializer jacksonSeial Jackson2JsonRedisSerializer (Object.class); 

        ObjectMapper new new ObjectMapper OM = (); 
        // specifies the sequence of domains, field, get and set, and a range modifier, ANY comprising both a private and public 
        om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 
        // the specified type of the input sequence, the non-final class must be modified, the modified final class, such as String, Integer, etc. will run out abnormal 
        om.enableDefaultTyping (ObjectMapper.DefaultTyping. NON_FINAL); 
        jacksonSeial.setObjectMapper (OM); 

        // value takes json serialization 
        template.setValueSerializer (jacksonSeial);  
        Key value // use StringRedisSerializer to serialize and deserialize the redis
        template.setKeySerializer (new new StringRedisSerializer ()); 

        // set the hash key and the value sequence pattern
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        //打开事务支持
        template.setEnableTransactionSupport(true);

        return template;
    }
}
Published 11 original articles · won praise 5 · Views 2761

Guess you like

Origin blog.csdn.net/weixin_41979668/article/details/103978121