|
1 | 1 | package com.binarywang.spring.starter.wxjava.open.config; |
2 | 2 |
|
3 | | -import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties; |
4 | | -import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; |
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; |
10 | | -import me.chanjar.weixin.open.api.WxOpenConfigStorage; |
11 | | -import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; |
12 | | -import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage; |
13 | | -import org.apache.commons.lang3.StringUtils; |
14 | | -import org.redisson.Redisson; |
15 | | -import org.redisson.api.RedissonClient; |
16 | | -import org.redisson.config.Config; |
17 | | -import org.redisson.config.TransportMode; |
18 | | -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
19 | | -import org.springframework.context.ApplicationContext; |
20 | | -import org.springframework.context.annotation.Bean; |
| 3 | +import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInMemoryConfigStorageConfiguration; |
| 4 | +import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedisConfigStorageConfiguration; |
| 5 | +import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedisTemplateConfigStorageConfiguration; |
| 6 | +import com.binarywang.spring.starter.wxjava.open.config.storage.WxOpenInRedissonConfigStorageConfiguration; |
21 | 7 | import org.springframework.context.annotation.Configuration; |
22 | | -import org.springframework.data.redis.core.StringRedisTemplate; |
23 | | -import redis.clients.jedis.JedisPool; |
24 | | -import redis.clients.jedis.JedisPoolConfig; |
| 8 | +import org.springframework.context.annotation.Import; |
25 | 9 |
|
26 | 10 | /** |
27 | 11 | * 微信公众号存储策略自动配置. |
28 | 12 | * |
29 | 13 | * @author someone |
30 | 14 | */ |
31 | 15 | @Configuration |
32 | | -@RequiredArgsConstructor |
| 16 | +@Import({ |
| 17 | + WxOpenInMemoryConfigStorageConfiguration.class, |
| 18 | + WxOpenInRedisTemplateConfigStorageConfiguration.class, |
| 19 | + WxOpenInRedisConfigStorageConfiguration.class, |
| 20 | + WxOpenInRedissonConfigStorageConfiguration.class |
| 21 | +}) |
33 | 22 | public class WxOpenStorageAutoConfiguration { |
34 | | - private final WxOpenProperties properties; |
35 | | - private final ApplicationContext applicationContext; |
36 | | - |
37 | | - @Bean |
38 | | - @ConditionalOnMissingBean(WxOpenConfigStorage.class) |
39 | | - public WxOpenConfigStorage wxOpenConfigStorage() { |
40 | | - WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); |
41 | | - WxOpenProperties.StorageType type = storage.getType(); |
42 | | - |
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(); |
52 | | - } |
53 | | - |
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()); |
61 | | - } |
62 | | - return config; |
63 | | - } |
64 | | - |
65 | | - private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() { |
66 | | - WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage(); |
67 | | - return config; |
68 | | - } |
69 | | - |
70 | | - private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() { |
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); |
77 | | - } |
78 | | - WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); |
79 | | - WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); |
80 | | - return config; |
81 | | - } |
82 | | - |
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); |
90 | | - } |
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()); |
100 | | - return config; |
101 | | - } |
102 | | - |
103 | | - |
104 | | - private JedisPool getJedisPool() { |
105 | | - WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); |
106 | | - RedisProperties redis = storage.getRedis(); |
107 | | - |
108 | | - JedisPoolConfig config = new JedisPoolConfig(); |
109 | | - if (redis.getMaxActive() != null) { |
110 | | - config.setMaxTotal(redis.getMaxActive()); |
111 | | - } |
112 | | - if (redis.getMaxIdle() != null) { |
113 | | - config.setMaxIdle(redis.getMaxIdle()); |
114 | | - } |
115 | | - if (redis.getMaxWaitMillis() != null) { |
116 | | - config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
117 | | - } |
118 | | - if (redis.getMinIdle() != null) { |
119 | | - config.setMinIdle(redis.getMinIdle()); |
120 | | - } |
121 | | - config.setTestOnBorrow(true); |
122 | | - config.setTestWhileIdle(true); |
123 | | - |
124 | | - JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), |
125 | | - redis.getTimeout(), redis.getPassword(), redis.getDatabase()); |
126 | | - return pool; |
127 | | - } |
128 | | - |
129 | | - private RedissonClient getRedissonClient() { |
130 | | - WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); |
131 | | - RedisProperties redis = storage.getRedis(); |
132 | | - |
133 | | - Config config = new Config(); |
134 | | - config.useSingleServer() |
135 | | - .setAddress("redis://" + redis.getHost() + ":" + redis.getPort()) |
136 | | - .setDatabase(redis.getDatabase()) |
137 | | - .setPassword(redis.getPassword()); |
138 | | - config.setTransportMode(TransportMode.NIO); |
139 | | - return Redisson.create(config); |
140 | | - } |
141 | 23 | } |
0 commit comments