提交 | 用户 | age
|
f3ad4b
|
1 |
package com.hx.redis; |
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 |
@Configuration |
|
15 |
@EnableCaching |
|
16 |
public class RedisConfig { |
|
17 |
@Bean |
|
18 |
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { |
|
19 |
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); |
|
20 |
template.setConnectionFactory(factory); |
|
21 |
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
|
22 |
ObjectMapper om = new ObjectMapper(); |
|
23 |
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
|
24 |
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
|
25 |
jackson2JsonRedisSerializer.setObjectMapper(om); |
|
26 |
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
|
27 |
template.setKeySerializer(stringRedisSerializer); |
|
28 |
template.setHashKeySerializer(stringRedisSerializer); |
|
29 |
template.setValueSerializer(jackson2JsonRedisSerializer); |
|
30 |
template.setHashValueSerializer(jackson2JsonRedisSerializer); |
|
31 |
template.afterPropertiesSet(); |
|
32 |
return template; |
|
33 |
} |
|
34 |
} |