| | |
| | | package me.zhengjie.config; |
| | | |
| | | import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties; |
| | | import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties; |
| | | import me.chanjar.weixin.cp.api.WxCpService; |
| | | import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; |
| | | import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; |
| | | import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl; |
| | | import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| | |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import redis.clients.jedis.JedisPool; |
| | | import redis.clients.jedis.JedisPoolConfig; |
| | | |
| | | /** |
| | | * @author baoLh |
| | |
| | | // redis缓存配置 |
| | | StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); |
| | | configStorage = new WxCpRedisTemplateConfigImpl(redisTemplate, wxCpProperties.getConfigStorage().getKeyPrefix()); |
| | | } else if (wxCpProperties.getConfigStorage().getType().equals(WxCpProperties.StorageType.jedis)) { |
| | | WxCpRedisProperties wxCpRedisProperties = wxCpProperties.getConfigStorage().getRedis(); |
| | | JedisPool jedisPool; |
| | | if (wxCpRedisProperties != null && StringUtils.isNotEmpty(wxCpRedisProperties.getHost())) { |
| | | jedisPool = getJedisPool(); |
| | | } else { |
| | | jedisPool = applicationContext.getBean(JedisPool.class); |
| | | } |
| | | configStorage = new WxCpJedisConfigImpl(jedisPool, wxCpProperties.getConfigStorage().getKeyPrefix()); |
| | | } else { |
| | | // 默认配置 |
| | | configStorage = new WxCpDefaultConfigImpl(); |
| | |
| | | |
| | | return wxCpService; |
| | | } |
| | | |
| | | private JedisPool getJedisPool() { |
| | | WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage(); |
| | | WxCpRedisProperties redis = storage.getRedis(); |
| | | |
| | | JedisPoolConfig config = new JedisPoolConfig(); |
| | | if (redis.getMaxActive() != null) { |
| | | config.setMaxTotal(redis.getMaxActive()); |
| | | } |
| | | if (redis.getMaxIdle() != null) { |
| | | config.setMaxIdle(redis.getMaxIdle()); |
| | | } |
| | | if (redis.getMaxWaitMillis() != null) { |
| | | config.setMaxWaitMillis(redis.getMaxWaitMillis()); |
| | | } |
| | | if (redis.getMinIdle() != null) { |
| | | config.setMinIdle(redis.getMinIdle()); |
| | | } |
| | | config.setTestOnBorrow(true); |
| | | config.setTestWhileIdle(true); |
| | | |
| | | return new JedisPool(config, redis.getHost(), redis.getPort(), |
| | | redis.getTimeout(), redis.getPassword(), redis.getDatabase()); |
| | | } |
| | | } |