chenjiahe
2024-09-27 1db5d379c60a96e277022c3096ca13cbc0d765f3
提交 | 用户 | age
826b66 1 package com.hx.redisson.config;
C 2
3 import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 import com.fasterxml.jackson.annotation.PropertyAccessor;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import org.springframework.cache.annotation.EnableCaching;
7 import org.springframework.context.annotation.Bean;
8 import org.springframework.context.annotation.Configuration;
9 import org.springframework.data.redis.connection.RedisConnectionFactory;
10 import org.springframework.data.redis.core.RedisTemplate;
11 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
12 import org.springframework.data.redis.serializer.StringRedisSerializer;
13
14 /**配置实例化
15  * CJH
16  */
17 @Configuration
18 public class RedissonConfig {
19     @Bean
20     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
21         RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
22         template.setConnectionFactory(factory);
23         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
24         ObjectMapper om = new ObjectMapper();
25         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
26         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
27         jackson2JsonRedisSerializer.setObjectMapper(om);
28         StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
29         template.setKeySerializer(stringRedisSerializer);
30         template.setHashKeySerializer(stringRedisSerializer);
31         template.setValueSerializer(jackson2JsonRedisSerializer);
32         template.setHashValueSerializer(jackson2JsonRedisSerializer);
33         template.afterPropertiesSet();
34         return template;
35     }
36 }