|
1 | 1 | package com.binarywang.spring.starter.wxjava.miniapp.config;
|
2 | 2 |
|
3 |
| -import cn.binarywang.wx.miniapp.api.WxMaService; |
4 |
| -import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl; |
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; |
8 |
| -import cn.binarywang.wx.miniapp.config.WxMaConfig; |
9 |
| -import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
10 |
| -import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl; |
11 |
| -import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType; |
12 |
| -import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties; |
13 | 3 | import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
|
14 |
| -import lombok.AllArgsConstructor; |
15 |
| -import me.chanjar.weixin.common.redis.JedisWxRedisOps; |
16 |
| -import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; |
17 |
| -import me.chanjar.weixin.common.redis.WxRedisOps; |
18 |
| -import org.apache.commons.lang3.StringUtils; |
19 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
20 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
21 |
| -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
22 | 4 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
23 |
| -import org.springframework.context.ApplicationContext; |
24 |
| -import org.springframework.context.annotation.Bean; |
25 | 5 | import org.springframework.context.annotation.Configuration;
|
26 |
| -import org.springframework.data.redis.core.StringRedisTemplate; |
27 |
| -import redis.clients.jedis.JedisPool; |
28 |
| -import redis.clients.jedis.JedisPoolConfig; |
| 6 | +import org.springframework.context.annotation.Import; |
29 | 7 |
|
30 | 8 | /**
|
31 | 9 | * 自动配置.
|
32 | 10 | *
|
33 | 11 | * @author <a href="https://github.com/binarywang">Binary Wang</a>
|
34 | 12 | * @date 2019-08-10
|
35 | 13 | */
|
36 |
| -@AllArgsConstructor |
37 | 14 | @Configuration
|
38 |
| -@ConditionalOnClass(WxMaService.class) |
39 | 15 | @EnableConfigurationProperties(WxMaProperties.class)
|
40 |
| -@ConditionalOnProperty(prefix = "wx.miniapp", value = "enabled", matchIfMissing = true) |
| 16 | +@Import({ |
| 17 | + WxMaStorageAutoConfiguration.class, |
| 18 | + WxMaServiceAutoConfiguration.class |
| 19 | +}) |
41 | 20 | public class WxMaAutoConfiguration {
|
42 |
| - |
43 |
| - private final WxMaProperties wxMaProperties; |
44 |
| - private final ApplicationContext applicationContext; |
45 |
| - |
46 |
| - /** |
47 |
| - * 小程序service. |
48 |
| - * |
49 |
| - * @return 小程序service |
50 |
| - */ |
51 |
| - @Bean |
52 |
| - @ConditionalOnMissingBean(WxMaService.class) |
53 |
| - public WxMaService service(WxMaConfig wxMaConfig) { |
54 |
| - HttpClientType httpClientType = wxMaProperties.getConfigStorage().getHttpClientType(); |
55 |
| - WxMaService wxMaService; |
56 |
| - switch (httpClientType) { |
57 |
| - case OkHttp: |
58 |
| - wxMaService = new WxMaServiceOkHttpImpl(); |
59 |
| - break; |
60 |
| - case JoddHttp: |
61 |
| - wxMaService = new WxMaServiceJoddHttpImpl(); |
62 |
| - break; |
63 |
| - case HttpClient: |
64 |
| - wxMaService = new WxMaServiceHttpClientImpl(); |
65 |
| - break; |
66 |
| - default: |
67 |
| - wxMaService = new WxMaServiceImpl(); |
68 |
| - break; |
69 |
| - } |
70 |
| - wxMaService.setWxMaConfig(wxMaConfig); |
71 |
| - return wxMaService; |
72 |
| - } |
73 |
| - |
74 |
| - @Bean |
75 |
| - @ConditionalOnMissingBean(WxMaConfig.class) |
76 |
| - public WxMaConfig wxMaConfig() { |
77 |
| - WxMaDefaultConfigImpl config; |
78 |
| - switch (wxMaProperties.getConfigStorage().getType()) { |
79 |
| - case Jedis: |
80 |
| - config = WxMaRedisBetterConfig.config(wxMaProperties, applicationContext); |
81 |
| - break; |
82 |
| - case RedisTemplate: |
83 |
| - config = wxMaRedisTemplateConfigStorage(); |
84 |
| - break; |
85 |
| - default: |
86 |
| - config = wxMaDefaultConfigStorage(); |
87 |
| - break; |
88 |
| - } |
89 |
| - |
90 |
| - config.setAppid(StringUtils.trimToNull(this.wxMaProperties.getAppid())); |
91 |
| - config.setSecret(StringUtils.trimToNull(this.wxMaProperties.getSecret())); |
92 |
| - config.setToken(StringUtils.trimToNull(this.wxMaProperties.getToken())); |
93 |
| - config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey())); |
94 |
| - config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat())); |
95 |
| - |
96 |
| - WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage(); |
97 |
| - config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); |
98 |
| - config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); |
99 |
| - config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); |
100 |
| - if (configStorageProperties.getHttpProxyPort() != null) { |
101 |
| - config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); |
102 |
| - } |
103 |
| - return config; |
104 |
| - } |
105 |
| - |
106 |
| - private WxMaDefaultConfigImpl wxMaDefaultConfigStorage() { |
107 |
| - return new WxMaDefaultConfigImpl(); |
108 |
| - } |
109 |
| - |
110 |
| - private static class WxMaRedisBetterConfig { |
111 |
| - |
112 |
| - private static WxMaDefaultConfigImpl config(WxMaProperties wxMaProperties, ApplicationContext context) { |
113 |
| - RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis(); |
114 |
| - JedisPool jedisPool; |
115 |
| - if (StringUtils.isNotEmpty(redisProperties.getHost())) { |
116 |
| - JedisPoolConfig config = new JedisPoolConfig(); |
117 |
| - if (redisProperties.getMaxActive() != null) { |
118 |
| - config.setMaxTotal(redisProperties.getMaxActive()); |
119 |
| - } |
120 |
| - if (redisProperties.getMaxIdle() != null) { |
121 |
| - config.setMaxIdle(redisProperties.getMaxIdle()); |
122 |
| - } |
123 |
| - if (redisProperties.getMaxWaitMillis() != null) { |
124 |
| - config.setMaxWaitMillis(redisProperties.getMaxWaitMillis()); |
125 |
| - } |
126 |
| - if (redisProperties.getMinIdle() != null) { |
127 |
| - config.setMinIdle(redisProperties.getMinIdle()); |
128 |
| - } |
129 |
| - config.setTestOnBorrow(true); |
130 |
| - config.setTestWhileIdle(true); |
131 |
| - |
132 |
| - jedisPool = new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), |
133 |
| - redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase()); |
134 |
| - } else { |
135 |
| - jedisPool = context.getBean(JedisPool.class); |
136 |
| - } |
137 |
| - WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
138 |
| - return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix()); |
139 |
| - } |
140 |
| - } |
141 |
| - |
142 |
| - |
143 |
| - private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage() { |
144 |
| - StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); |
145 |
| - WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); |
146 |
| - return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix()); |
147 |
| - } |
148 | 21 | }
|
0 commit comments