1
1
package com .ziniu ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonAutoDetect ;
4
+ import com .fasterxml .jackson .annotation .PropertyAccessor ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
3
6
import com .ulisesbocchio .jasyptspringboot .annotation .EnableEncryptableProperties ;
4
7
import org .apache .activemq .command .ActiveMQQueue ;
5
8
import org .springframework .boot .SpringApplication ;
6
9
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
10
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
7
11
import org .springframework .boot .web .servlet .ServletComponentScan ;
8
12
import org .springframework .context .annotation .Bean ;
9
13
import org .springframework .context .annotation .ComponentScan ;
10
14
import org .springframework .context .annotation .Configuration ;
15
+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
16
+ import org .springframework .data .redis .core .RedisTemplate ;
17
+ import org .springframework .data .redis .serializer .Jackson2JsonRedisSerializer ;
18
+ import org .springframework .data .redis .serializer .StringRedisSerializer ;
11
19
12
20
13
21
import javax .jms .Queue ;
22
+ import java .net .UnknownHostException ;
14
23
15
24
/**
16
25
* Copyright © 2016年 author. All rights reserved.
17
26
*
18
27
* @Author 临江仙 hxqiuxm@163.com
19
28
* @Date 2017/3/1 0001 10:39
20
29
*/
21
- @ Configuration
22
- @ EnableAutoConfiguration
23
- @ ComponentScan (basePackages = {"com.ziniu" })
30
+ //
31
+ @ SpringBootApplication
24
32
@ EnableEncryptableProperties
25
33
@ ServletComponentScan
26
34
public class Application {
@@ -31,7 +39,25 @@ public Queue queue() {
31
39
return new ActiveMQQueue ("sample.queue" );
32
40
}
33
41
42
+ @ Bean
43
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
44
+ public RedisTemplate <Object , Object > redisTemplate (RedisConnectionFactory redisConnectionFactory )
45
+ throws UnknownHostException {
46
+ RedisTemplate <Object , Object > template = new RedisTemplate <Object , Object >();
47
+ template .setConnectionFactory (redisConnectionFactory );
48
+
49
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object .class );
50
+ ObjectMapper om = new ObjectMapper ();
51
+ om .setVisibility (PropertyAccessor .ALL , JsonAutoDetect .Visibility .ANY );
52
+ om .enableDefaultTyping (ObjectMapper .DefaultTyping .NON_FINAL );
53
+ jackson2JsonRedisSerializer .setObjectMapper (om );
34
54
55
+ template .setValueSerializer (jackson2JsonRedisSerializer ); //1 值采用jackson2JsonRedisSerializer序列化
56
+ template .setKeySerializer (new StringRedisSerializer ()); //2 键采用StringRedisSerializer序列化
57
+
58
+ template .afterPropertiesSet ();
59
+ return template ;
60
+ }
35
61
36
62
public static void main (String [] args ) {
37
63
0 commit comments