|
| 1 | +package com.binarywang.spring.starter.wxjava.mp; |
| 2 | + |
| 3 | +import me.chanjar.weixin.mp.api.WxMpConfigStorage; |
| 4 | +import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; |
| 5 | +import me.chanjar.weixin.mp.api.WxMpInRedisConfigStorage; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import redis.clients.jedis.JedisPool; |
| 11 | +import redis.clients.jedis.JedisPoolConfig; |
| 12 | + |
| 13 | +/** |
| 14 | + * 微信公众号存储策略自动配置 |
| 15 | + */ |
| 16 | +@Configuration |
| 17 | +public class WxMpStorageAutoConfiguration { |
| 18 | + |
| 19 | + @Autowired |
| 20 | + private WxMpProperties properties; |
| 21 | + |
| 22 | + @Autowired(required = false) |
| 23 | + private JedisPool jedisPool; |
| 24 | + |
| 25 | + @Bean |
| 26 | + @ConditionalOnMissingBean(WxMpConfigStorage.class) |
| 27 | + public WxMpConfigStorage wxMpInMemoryConfigStorage() { |
| 28 | + WxMpProperties.ConfigStorage storage = properties.getConfigStorage(); |
| 29 | + WxMpProperties.StorageType type = storage.getType(); |
| 30 | + |
| 31 | + if (type == WxMpProperties.StorageType.redis) { |
| 32 | + return getWxMpInRedisConfigStorage(); |
| 33 | + } |
| 34 | + return getWxMpInMemoryConfigStorage(); |
| 35 | + } |
| 36 | + |
| 37 | + private WxMpInMemoryConfigStorage getWxMpInMemoryConfigStorage() { |
| 38 | + WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); |
| 39 | + setWxMpInfo(config); |
| 40 | + return config; |
| 41 | + } |
| 42 | + |
| 43 | + private WxMpInRedisConfigStorage getWxMpInRedisConfigStorage() { |
| 44 | + JedisPool poolToUse = jedisPool; |
| 45 | + if (poolToUse == null) { |
| 46 | + poolToUse = getJedisPool(); |
| 47 | + } |
| 48 | + WxMpInRedisConfigStorage config = new WxMpInRedisConfigStorage(poolToUse); |
| 49 | + setWxMpInfo(config); |
| 50 | + return config; |
| 51 | + } |
| 52 | + |
| 53 | + private void setWxMpInfo(WxMpInMemoryConfigStorage config) { |
| 54 | + config.setAppId(properties.getAppId()); |
| 55 | + config.setSecret(properties.getSecret()); |
| 56 | + config.setToken(properties.getToken()); |
| 57 | + config.setAesKey(properties.getAesKey()); |
| 58 | + } |
| 59 | + |
| 60 | + private JedisPool getJedisPool() { |
| 61 | + WxMpProperties.ConfigStorage storage = properties.getConfigStorage(); |
| 62 | + RedisProperties redis = storage.getRedis(); |
| 63 | + |
| 64 | + JedisPoolConfig config = new JedisPoolConfig(); |
| 65 | + if (redis.getMaxActive() != null) { |
| 66 | + config.setMaxTotal(redis.getMaxActive()); |
| 67 | + } |
| 68 | + if (redis.getMaxIdle() != null) { |
| 69 | + config.setMaxIdle(redis.getMaxIdle()); |
| 70 | + } |
| 71 | + if (redis.getMaxWaitMillis() != null) { |
| 72 | + config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
| 73 | + } |
| 74 | + if (redis.getMinIdle() != null) { |
| 75 | + config.setMinIdle(redis.getMinIdle()); |
| 76 | + } |
| 77 | + config.setTestOnBorrow(true); |
| 78 | + config.setTestWhileIdle(true); |
| 79 | + |
| 80 | + JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), |
| 81 | + redis.getTimeout(), redis.getPassword(), redis.getDatabase()); |
| 82 | + return pool; |
| 83 | + } |
| 84 | +} |
0 commit comments