11package com .binarywang .spring .starter .wxjava .miniapp .config ;
22
33import cn .binarywang .wx .miniapp .api .WxMaService ;
4+ import cn .binarywang .wx .miniapp .api .impl .WxMaServiceHttpClientImpl ;
45import 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 ;
58import cn .binarywang .wx .miniapp .config .WxMaConfig ;
69import cn .binarywang .wx .miniapp .config .impl .WxMaDefaultConfigImpl ;
710import cn .binarywang .wx .miniapp .config .impl .WxMaRedisBetterConfigImpl ;
11+ import com .binarywang .spring .starter .wxjava .miniapp .enums .HttpClientType ;
812import com .binarywang .spring .starter .wxjava .miniapp .properties .WxMaProperties ;
913import lombok .AllArgsConstructor ;
1014import me .chanjar .weixin .common .redis .JedisWxRedisOps ;
@@ -46,22 +50,35 @@ public class WxMaAutoConfiguration {
4650 @ Bean
4751 @ ConditionalOnMissingBean (WxMaService .class )
4852 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 ;
5266 }
5367
5468 @ Bean
5569 @ ConditionalOnMissingBean (WxMaConfig .class )
5670 public WxMaConfig wxMaConfig () {
57- WxMaProperties .StorageType type = wxMaProperties .getConfigStorage ().getType ();
5871 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 ;
6582 }
6683
6784 config .setAppid (StringUtils .trimToNull (this .wxMaProperties .getAppid ()));
@@ -80,52 +97,42 @@ public WxMaConfig wxMaConfig() {
8097 return config ;
8198 }
8299
83- private WxMaDefaultConfigImpl wxMaInMemoryConfigStorage () {
84- WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl ();
85- return config ;
100+ private WxMaDefaultConfigImpl wxMaDefaultConfigStorage () {
101+ return new WxMaDefaultConfigImpl ();
86102 }
87103
88- private WxMaDefaultConfigImpl wxMaInJedisConfigStorage () {
104+ private WxMaDefaultConfigImpl wxMaJedisConfigStorage () {
89105 WxMaProperties .RedisProperties redisProperties = wxMaProperties .getConfigStorage ().getRedis ();
90106 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 ());
93126 } else {
94127 jedisPool = applicationContext .getBean (JedisPool .class );
95128 }
96129 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 ());
99131 }
100132
101- private WxMaDefaultConfigImpl wxMaInRedisTemplateConfigStorage () {
133+ private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage () {
102134 StringRedisTemplate redisTemplate = applicationContext .getBean (StringRedisTemplate .class );
103135 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 ());
130137 }
131138}
0 commit comments