1
1
package com .binarywang .spring .starter .wxjava .mp .config ;
2
2
3
- import com .binarywang .spring .starter .wxjava .mp .properties . RedisProperties ;
3
+ import com .binarywang .spring .starter .wxjava .mp .extend . RedisTemplateWxMpRedisOps ;
4
4
import com .binarywang .spring .starter .wxjava .mp .properties .WxMpProperties ;
5
5
import lombok .RequiredArgsConstructor ;
6
6
import me .chanjar .weixin .mp .config .WxMpConfigStorage ;
7
7
import me .chanjar .weixin .mp .config .impl .WxMpDefaultConfigImpl ;
8
8
import me .chanjar .weixin .mp .config .impl .WxMpRedisConfigImpl ;
9
+ import me .chanjar .weixin .mp .config .redis .JedisWxMpRedisOps ;
10
+ import me .chanjar .weixin .mp .config .redis .WxMpRedisOps ;
11
+ import org .apache .commons .lang3 .StringUtils ;
12
+ import org .springframework .beans .factory .annotation .Value ;
9
13
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
10
14
import org .springframework .context .ApplicationContext ;
11
15
import org .springframework .context .annotation .Bean ;
12
16
import org .springframework .context .annotation .Configuration ;
17
+ import org .springframework .data .redis .core .StringRedisTemplate ;
13
18
import redis .clients .jedis .JedisPool ;
14
19
import redis .clients .jedis .JedisPoolConfig ;
15
20
22
27
@ RequiredArgsConstructor
23
28
public class WxMpStorageAutoConfiguration {
24
29
25
- private final WxMpProperties properties ;
26
30
private final ApplicationContext applicationContext ;
27
31
32
+ private final WxMpProperties wxMpProperties ;
33
+
34
+ @ Value ("${wx.mp.config-storage.redis.host:" )
35
+ private String redisHost ;
36
+
37
+ @ Value ("${wx.mp.configStorage.redis.host:" )
38
+ private String redisHost2 ;
39
+
28
40
@ Bean
29
41
@ ConditionalOnMissingBean (WxMpConfigStorage .class )
30
- public WxMpConfigStorage wxMpInMemoryConfigStorage () {
31
- WxMpProperties .ConfigStorage storage = properties .getConfigStorage ();
32
- WxMpProperties .StorageType type = storage .getType ();
33
-
34
- if (type == WxMpProperties .StorageType .redis ) {
35
- return getWxMpInRedisConfigStorage ();
42
+ public WxMpConfigStorage wxMpConfigStorage () {
43
+ WxMpProperties .StorageType type = wxMpProperties .getConfigStorage ().getType ();
44
+ WxMpConfigStorage config ;
45
+ if (type == WxMpProperties .StorageType .redis || type == WxMpProperties .StorageType .jedis ) {
46
+ config = wxMpInJedisConfigStorage ();
47
+ } else if (type == WxMpProperties .StorageType .redistemplate ) {
48
+ config = wxMpInRedisTemplateConfigStorage ();
49
+ } else {
50
+ config = wxMpInMemoryConfigStorage ();
36
51
}
37
- return getWxMpInMemoryConfigStorage () ;
52
+ return config ;
38
53
}
39
54
40
- private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage () {
55
+ private WxMpConfigStorage wxMpInMemoryConfigStorage () {
41
56
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl ();
42
57
setWxMpInfo (config );
43
58
return config ;
44
59
}
45
60
46
- private WxMpRedisConfigImpl getWxMpInRedisConfigStorage () {
47
- RedisProperties .ImplType implType = properties .getConfigStorage ().getRedis ().getImpl ();
48
- boolean reuseBean = properties .getConfigStorage ().getRedis ().isReuseBean ();
49
- if (implType == RedisProperties .ImplType .jedis ) {
50
- JedisPool pool = null ;
51
- if (reuseBean ) {
52
- pool = getBean (JedisPool .class );
53
- }
54
- if (pool == null ) {
55
- pool = getJedisPool ();
56
- }
57
- WxMpRedisConfigImpl config = new WxMpRedisConfigImpl (pool );
58
- setWxMpInfo (config );
59
- return config ;
61
+ private WxMpConfigStorage wxMpInJedisConfigStorage () {
62
+ JedisPool jedisPool ;
63
+ if (StringUtils .isNotEmpty (redisHost ) || StringUtils .isNotEmpty (redisHost2 )) {
64
+ jedisPool = getJedisPool ();
65
+ } else {
66
+ jedisPool = applicationContext .getBean (JedisPool .class );
60
67
}
61
- throw new UnsupportedOperationException ();
68
+ WxMpRedisOps redisOps = new JedisWxMpRedisOps (jedisPool );
69
+ WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl (redisOps , wxMpProperties .getConfigStorage ().getKeyPrefix ());
70
+ setWxMpInfo (wxMpRedisConfig );
71
+ return wxMpRedisConfig ;
72
+ }
73
+
74
+ private WxMpConfigStorage wxMpInRedisTemplateConfigStorage () {
75
+ StringRedisTemplate redisTemplate = applicationContext .getBean (StringRedisTemplate .class );
76
+ WxMpRedisOps redisOps = new RedisTemplateWxMpRedisOps (redisTemplate );
77
+ WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl (redisOps , wxMpProperties .getConfigStorage ().getKeyPrefix ());
78
+ setWxMpInfo (wxMpRedisConfig );
79
+ return wxMpRedisConfig ;
62
80
}
63
81
64
82
private void setWxMpInfo (WxMpDefaultConfigImpl config ) {
83
+ WxMpProperties properties = wxMpProperties ;
84
+ WxMpProperties .ConfigStorage configStorageProperties = properties .getConfigStorage ();
65
85
config .setAppId (properties .getAppId ());
66
86
config .setSecret (properties .getSecret ());
67
87
config .setToken (properties .getToken ());
68
88
config .setAesKey (properties .getAesKey ());
89
+
90
+ config .setHttpProxyHost (configStorageProperties .getHttpProxyHost ());
91
+ config .setHttpProxyUsername (configStorageProperties .getHttpProxyUsername ());
92
+ config .setHttpProxyPassword (configStorageProperties .getHttpProxyPassword ());
93
+ if (configStorageProperties .getHttpProxyPort () != null ) {
94
+ config .setHttpProxyPort (configStorageProperties .getHttpProxyPort ());
95
+ }
69
96
}
70
97
71
98
private JedisPool getJedisPool () {
72
- WxMpProperties .ConfigStorage storage = properties .getConfigStorage ();
73
- RedisProperties redis = storage .getRedis ();
99
+ WxMpProperties .ConfigStorage storage = wxMpProperties .getConfigStorage ();
100
+ WxMpProperties . RedisProperties redis = storage .getRedis ();
74
101
75
102
JedisPoolConfig config = new JedisPoolConfig ();
76
103
if (redis .getMaxActive () != null ) {
@@ -91,11 +118,4 @@ private JedisPool getJedisPool() {
91
118
return new JedisPool (config , redis .getHost (), redis .getPort (), redis .getTimeout (), redis .getPassword (),
92
119
redis .getDatabase ());
93
120
}
94
-
95
- private <T > T getBean (Class <T > clazz ) {
96
- if (this .applicationContext .getBeanNamesForType (clazz , false , false ).length > 0 ) {
97
- return this .applicationContext .getBean (clazz );
98
- }
99
- return null ;
100
- }
101
121
}
0 commit comments