|
3 | 3 | import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties;
|
4 | 4 | import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
|
5 | 5 | import lombok.RequiredArgsConstructor;
|
| 6 | +import me.chanjar.weixin.common.redis.JedisWxRedisOps; |
| 7 | +import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; |
| 8 | +import me.chanjar.weixin.common.redis.RedissonWxRedisOps; |
| 9 | +import me.chanjar.weixin.common.redis.WxRedisOps; |
6 | 10 | import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
7 | 11 | import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
8 | 12 | import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
|
9 |
| -import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage; |
10 | 13 | import org.apache.commons.lang3.StringUtils;
|
11 | 14 | import org.redisson.Redisson;
|
12 | 15 | import org.redisson.api.RedissonClient;
|
13 | 16 | import org.redisson.config.Config;
|
14 | 17 | import org.redisson.config.TransportMode;
|
15 |
| -import org.springframework.beans.factory.annotation.Autowired; |
16 |
| -import org.springframework.beans.factory.annotation.Value; |
17 | 18 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 19 | +import org.springframework.context.ApplicationContext; |
18 | 20 | import org.springframework.context.annotation.Bean;
|
19 | 21 | import org.springframework.context.annotation.Configuration;
|
| 22 | +import org.springframework.data.redis.core.StringRedisTemplate; |
20 | 23 | import redis.clients.jedis.JedisPool;
|
21 | 24 | import redis.clients.jedis.JedisPoolConfig;
|
22 | 25 |
|
|
29 | 32 | @RequiredArgsConstructor
|
30 | 33 | public class WxOpenStorageAutoConfiguration {
|
31 | 34 | private final WxOpenProperties properties;
|
32 |
| - |
33 |
| - @Autowired(required = false) |
34 |
| - private JedisPool jedisPool; |
35 |
| - |
36 |
| - @Autowired(required = false) |
37 |
| - private RedissonClient redissonClient; |
38 |
| - |
39 |
| - @Value("${wx.open.config-storage.redis.host:}") |
40 |
| - private String redisHost; |
| 35 | + private final ApplicationContext applicationContext; |
41 | 36 |
|
42 | 37 | @Bean
|
43 | 38 | @ConditionalOnMissingBean(WxOpenConfigStorage.class)
|
44 | 39 | public WxOpenConfigStorage wxOpenConfigStorage() {
|
45 | 40 | WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
|
46 | 41 | WxOpenProperties.StorageType type = storage.getType();
|
47 | 42 |
|
48 |
| - if (type == WxOpenProperties.StorageType.redis) { |
49 |
| - return getWxOpenInRedisConfigStorage(); |
| 43 | + WxOpenInMemoryConfigStorage config; |
| 44 | + if (type == WxOpenProperties.StorageType.redis || type == WxOpenProperties.StorageType.jedis) { |
| 45 | + config = getWxOpenInRedisConfigStorage(); |
| 46 | + } else if (type == WxOpenProperties.StorageType.redisson) { |
| 47 | + config = getWxOpenInRedissonConfigStorage(); |
| 48 | + } else if (type == WxOpenProperties.StorageType.redistemplate) { |
| 49 | + config = getWxOpenInRedisTemplateConfigStorage(); |
| 50 | + } else { |
| 51 | + config = getWxOpenInMemoryConfigStorage(); |
50 | 52 | }
|
51 | 53 |
|
52 |
| - if (type == WxOpenProperties.StorageType.jedis) { |
53 |
| - return getWxOpenInRedisConfigStorage(); |
54 |
| - } |
55 |
| - |
56 |
| - if (type == WxOpenProperties.StorageType.redisson) { |
57 |
| - return getWxOpenInRedissonConfigStorage(); |
| 54 | + WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); |
| 55 | + config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); |
| 56 | + config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); |
| 57 | + config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); |
| 58 | + config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); |
| 59 | + if (configStorageProperties.getHttpProxyPort() != null) { |
| 60 | + config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); |
58 | 61 | }
|
59 |
| - return getWxOpenInMemoryConfigStorage(); |
| 62 | + return config; |
60 | 63 | }
|
61 | 64 |
|
62 | 65 | private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
|
63 | 66 | WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
|
64 |
| - config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); |
65 | 67 | return config;
|
66 | 68 | }
|
67 | 69 |
|
68 | 70 | private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
|
69 |
| - JedisPool poolToUse = jedisPool; |
70 |
| - if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) { |
71 |
| - poolToUse = getJedisPool(); |
| 71 | + RedisProperties redisProperties = properties.getConfigStorage().getRedis(); |
| 72 | + JedisPool jedisPool; |
| 73 | + if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { |
| 74 | + jedisPool = getJedisPool(); |
| 75 | + } else { |
| 76 | + jedisPool = applicationContext.getBean(JedisPool.class); |
72 | 77 | }
|
73 |
| - WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse, properties.getConfigStorage().getKeyPrefix()); |
74 |
| - config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); |
| 78 | + WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
| 79 | + WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); |
75 | 80 | return config;
|
76 | 81 | }
|
77 | 82 |
|
78 |
| - private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() { |
79 |
| - RedissonClient redissonClientToUse = this.redissonClient; |
80 |
| - if (redissonClient == null) { |
81 |
| - redissonClientToUse = getRedissonClient(); |
| 83 | + private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() { |
| 84 | + RedisProperties redisProperties = properties.getConfigStorage().getRedis(); |
| 85 | + RedissonClient redissonClient; |
| 86 | + if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { |
| 87 | + redissonClient = getRedissonClient(); |
| 88 | + } else { |
| 89 | + redissonClient = applicationContext.getBean(RedissonClient.class); |
82 | 90 | }
|
83 |
| - WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse, properties.getConfigStorage().getKeyPrefix()); |
84 |
| - config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey()); |
| 91 | + WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient); |
| 92 | + WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); |
| 93 | + return config; |
| 94 | + } |
| 95 | + |
| 96 | + private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() { |
| 97 | + StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); |
| 98 | + WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); |
| 99 | + WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); |
85 | 100 | return config;
|
86 | 101 | }
|
87 | 102 |
|
|
0 commit comments