1
1
package com .binarywang .spring .starter .wxjava .miniapp .config ;
2
2
3
3
import cn .binarywang .wx .miniapp .api .WxMaService ;
4
+ import cn .binarywang .wx .miniapp .api .impl .WxMaServiceHttpClientImpl ;
4
5
import cn .binarywang .wx .miniapp .api .impl .WxMaServiceImpl ;
6
+ import cn .binarywang .wx .miniapp .api .impl .WxMaServiceJoddHttpImpl ;
7
+ import cn .binarywang .wx .miniapp .api .impl .WxMaServiceOkHttpImpl ;
5
8
import cn .binarywang .wx .miniapp .config .WxMaConfig ;
6
9
import cn .binarywang .wx .miniapp .config .impl .WxMaDefaultConfigImpl ;
7
10
import cn .binarywang .wx .miniapp .config .impl .WxMaRedisBetterConfigImpl ;
11
+ import com .binarywang .spring .starter .wxjava .miniapp .enums .HttpClientType ;
8
12
import com .binarywang .spring .starter .wxjava .miniapp .properties .WxMaProperties ;
9
13
import lombok .AllArgsConstructor ;
10
14
import me .chanjar .weixin .common .redis .JedisWxRedisOps ;
@@ -46,22 +50,35 @@ public class WxMaAutoConfiguration {
46
50
@ Bean
47
51
@ ConditionalOnMissingBean (WxMaService .class )
48
52
public WxMaService service (WxMaConfig wxMaConfig ) {
49
- final WxMaServiceImpl service = new WxMaServiceImpl ();
50
- service .setWxMaConfig (wxMaConfig );
51
- return service ;
53
+ HttpClientType httpClientType = wxMaProperties .getConfigStorage ().getHttpClientType ();
54
+ WxMaService wxMaService ;
55
+ if (httpClientType == HttpClientType .OkHttp ) {
56
+ wxMaService = new WxMaServiceOkHttpImpl ();
57
+ } else if (httpClientType == HttpClientType .JoddHttp ) {
58
+ wxMaService = new WxMaServiceJoddHttpImpl ();
59
+ } else if (httpClientType == HttpClientType .HttpClient ) {
60
+ wxMaService = new WxMaServiceHttpClientImpl ();
61
+ } else {
62
+ wxMaService = new WxMaServiceImpl ();
63
+ }
64
+ wxMaService .setWxMaConfig (wxMaConfig );
65
+ return wxMaService ;
52
66
}
53
67
54
68
@ Bean
55
69
@ ConditionalOnMissingBean (WxMaConfig .class )
56
70
public WxMaConfig wxMaConfig () {
57
- WxMaProperties .StorageType type = wxMaProperties .getConfigStorage ().getType ();
58
71
WxMaDefaultConfigImpl config ;
59
- if (type == WxMaProperties .StorageType .jedis ) {
60
- config = wxMaInJedisConfigStorage ();
61
- } else if (type == WxMaProperties .StorageType .redistemplate ) {
62
- config = wxMaInRedisTemplateConfigStorage ();
63
- } else {
64
- config = wxMaInMemoryConfigStorage ();
72
+ switch (wxMaProperties .getConfigStorage ().getType ()) {
73
+ case Jedis :
74
+ config = wxMaJedisConfigStorage ();
75
+ break ;
76
+ case RedisTemplate :
77
+ config = wxMaRedisTemplateConfigStorage ();
78
+ break ;
79
+ default :
80
+ config = wxMaDefaultConfigStorage ();
81
+ break ;
65
82
}
66
83
67
84
config .setAppid (StringUtils .trimToNull (this .wxMaProperties .getAppid ()));
@@ -80,52 +97,42 @@ public WxMaConfig wxMaConfig() {
80
97
return config ;
81
98
}
82
99
83
- private WxMaDefaultConfigImpl wxMaInMemoryConfigStorage () {
84
- WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl ();
85
- return config ;
100
+ private WxMaDefaultConfigImpl wxMaDefaultConfigStorage () {
101
+ return new WxMaDefaultConfigImpl ();
86
102
}
87
103
88
- private WxMaDefaultConfigImpl wxMaInJedisConfigStorage () {
104
+ private WxMaDefaultConfigImpl wxMaJedisConfigStorage () {
89
105
WxMaProperties .RedisProperties redisProperties = wxMaProperties .getConfigStorage ().getRedis ();
90
106
JedisPool jedisPool ;
91
- if (redisProperties != null && StringUtils .isNotEmpty (redisProperties .getHost ())) {
92
- jedisPool = getJedisPool ();
107
+ if (StringUtils .isNotEmpty (redisProperties .getHost ())) {
108
+ JedisPoolConfig config = new JedisPoolConfig ();
109
+ if (redisProperties .getMaxActive () != null ) {
110
+ config .setMaxTotal (redisProperties .getMaxActive ());
111
+ }
112
+ if (redisProperties .getMaxIdle () != null ) {
113
+ config .setMaxIdle (redisProperties .getMaxIdle ());
114
+ }
115
+ if (redisProperties .getMaxWaitMillis () != null ) {
116
+ config .setMaxWaitMillis (redisProperties .getMaxWaitMillis ());
117
+ }
118
+ if (redisProperties .getMinIdle () != null ) {
119
+ config .setMinIdle (redisProperties .getMinIdle ());
120
+ }
121
+ config .setTestOnBorrow (true );
122
+ config .setTestWhileIdle (true );
123
+
124
+ jedisPool = new JedisPool (config , redisProperties .getHost (), redisProperties .getPort (),
125
+ redisProperties .getTimeout (), redisProperties .getPassword (), redisProperties .getDatabase ());
93
126
} else {
94
127
jedisPool = applicationContext .getBean (JedisPool .class );
95
128
}
96
129
WxRedisOps redisOps = new JedisWxRedisOps (jedisPool );
97
- WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl (redisOps , wxMaProperties .getConfigStorage ().getKeyPrefix ());
98
- return wxMaRedisConfig ;
130
+ return new WxMaRedisBetterConfigImpl (redisOps , wxMaProperties .getConfigStorage ().getKeyPrefix ());
99
131
}
100
132
101
- private WxMaDefaultConfigImpl wxMaInRedisTemplateConfigStorage () {
133
+ private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage () {
102
134
StringRedisTemplate redisTemplate = applicationContext .getBean (StringRedisTemplate .class );
103
135
WxRedisOps redisOps = new RedisTemplateWxRedisOps (redisTemplate );
104
- WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl (redisOps , wxMaProperties .getConfigStorage ().getKeyPrefix ());
105
- return wxMaRedisConfig ;
106
- }
107
-
108
- private JedisPool getJedisPool () {
109
- WxMaProperties .ConfigStorage storage = wxMaProperties .getConfigStorage ();
110
- WxMaProperties .RedisProperties redis = storage .getRedis ();
111
-
112
- JedisPoolConfig config = new JedisPoolConfig ();
113
- if (redis .getMaxActive () != null ) {
114
- config .setMaxTotal (redis .getMaxActive ());
115
- }
116
- if (redis .getMaxIdle () != null ) {
117
- config .setMaxIdle (redis .getMaxIdle ());
118
- }
119
- if (redis .getMaxWaitMillis () != null ) {
120
- config .setMaxWaitMillis (redis .getMaxWaitMillis ());
121
- }
122
- if (redis .getMinIdle () != null ) {
123
- config .setMinIdle (redis .getMinIdle ());
124
- }
125
- config .setTestOnBorrow (true );
126
- config .setTestWhileIdle (true );
127
-
128
- return new JedisPool (config , redis .getHost (), redis .getPort (), redis .getTimeout (), redis .getPassword (),
129
- redis .getDatabase ());
136
+ return new WxMaRedisBetterConfigImpl (redisOps , wxMaProperties .getConfigStorage ().getKeyPrefix ());
130
137
}
131
138
}
0 commit comments