mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	🎨 优化重构并统一公众号和小程序的spring boot starter部分配置类和属性
This commit is contained in:
		| @ -1,7 +1,7 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.config; | ||||
|  | ||||
| import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType; | ||||
| import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; | ||||
| import me.chanjar.weixin.common.api.WxOcrService; | ||||
| import me.chanjar.weixin.mp.api.*; | ||||
| import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl; | ||||
| import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | ||||
| @ -23,16 +23,21 @@ public class WxMpServiceAutoConfiguration { | ||||
|   @Bean | ||||
|   @ConditionalOnMissingBean | ||||
|   public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) { | ||||
|     WxMpProperties.HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType(); | ||||
|     HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType(); | ||||
|     WxMpService wxMpService; | ||||
|     if (httpClientType == WxMpProperties.HttpClientType.okhttp) { | ||||
|       wxMpService = newWxMpServiceOkHttpImpl(); | ||||
|     } else if (httpClientType == WxMpProperties.HttpClientType.joddhttp) { | ||||
|       wxMpService = newWxMpServiceJoddHttpImpl(); | ||||
|     } else if (httpClientType == WxMpProperties.HttpClientType.httpclient) { | ||||
|       wxMpService = newWxMpServiceHttpClientImpl(); | ||||
|     } else { | ||||
|       wxMpService = newWxMpServiceImpl(); | ||||
|     switch (httpClientType) { | ||||
|       case OkHttp: | ||||
|         wxMpService = newWxMpServiceOkHttpImpl(); | ||||
|         break; | ||||
|       case JoddHttp: | ||||
|         wxMpService = newWxMpServiceJoddHttpImpl(); | ||||
|         break; | ||||
|       case HttpClient: | ||||
|         wxMpService = newWxMpServiceHttpClientImpl(); | ||||
|         break; | ||||
|       default: | ||||
|         wxMpService = newWxMpServiceImpl(); | ||||
|         break; | ||||
|     } | ||||
|  | ||||
|     wxMpService.setWxMpConfigStorage(configStorage); | ||||
|  | ||||
| @ -1,5 +1,7 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.config; | ||||
|  | ||||
| import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; | ||||
| import com.binarywang.spring.starter.wxjava.mp.enums.StorageType; | ||||
| import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import me.chanjar.weixin.common.redis.JedisWxRedisOps; | ||||
| @ -26,7 +28,6 @@ import redis.clients.jedis.JedisPoolConfig; | ||||
| @Configuration | ||||
| @RequiredArgsConstructor | ||||
| public class WxMpStorageAutoConfiguration { | ||||
|  | ||||
|   private final ApplicationContext applicationContext; | ||||
|  | ||||
|   private final WxMpProperties wxMpProperties; | ||||
| @ -40,25 +41,29 @@ public class WxMpStorageAutoConfiguration { | ||||
|   @Bean | ||||
|   @ConditionalOnMissingBean(WxMpConfigStorage.class) | ||||
|   public WxMpConfigStorage wxMpConfigStorage() { | ||||
|     WxMpProperties.StorageType type = wxMpProperties.getConfigStorage().getType(); | ||||
|     StorageType type = wxMpProperties.getConfigStorage().getType(); | ||||
|     WxMpConfigStorage config; | ||||
|     if (type == WxMpProperties.StorageType.redis || type == WxMpProperties.StorageType.jedis) { | ||||
|       config = wxMpInJedisConfigStorage(); | ||||
|     } else if (type == WxMpProperties.StorageType.redistemplate) { | ||||
|       config = wxMpInRedisTemplateConfigStorage(); | ||||
|     } else { | ||||
|       config = wxMpInMemoryConfigStorage(); | ||||
|     switch (type) { | ||||
|       case Jedis: | ||||
|         config = jedisConfigStorage(); | ||||
|         break; | ||||
|       case RedisTemplate: | ||||
|         config = redisTemplateConfigStorage(); | ||||
|         break; | ||||
|       default: | ||||
|         config = defaultConfigStorage(); | ||||
|         break; | ||||
|     } | ||||
|     return config; | ||||
|   } | ||||
|  | ||||
|   private WxMpConfigStorage wxMpInMemoryConfigStorage() { | ||||
|   private WxMpConfigStorage defaultConfigStorage() { | ||||
|     WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); | ||||
|     setWxMpInfo(config); | ||||
|     return config; | ||||
|   } | ||||
|  | ||||
|   private WxMpConfigStorage wxMpInJedisConfigStorage() { | ||||
|   private WxMpConfigStorage jedisConfigStorage() { | ||||
|     JedisPool jedisPool; | ||||
|     if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) { | ||||
|       jedisPool = getJedisPool(); | ||||
| @ -71,7 +76,7 @@ public class WxMpStorageAutoConfiguration { | ||||
|     return wxMpRedisConfig; | ||||
|   } | ||||
|  | ||||
|   private WxMpConfigStorage wxMpInRedisTemplateConfigStorage() { | ||||
|   private WxMpConfigStorage redisTemplateConfigStorage() { | ||||
|     StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); | ||||
|     WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); | ||||
|     WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix()); | ||||
| @ -97,7 +102,7 @@ public class WxMpStorageAutoConfiguration { | ||||
|  | ||||
|   private JedisPool getJedisPool() { | ||||
|     WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage(); | ||||
|     WxMpProperties.RedisProperties redis = storage.getRedis(); | ||||
|     RedisProperties redis = storage.getRedis(); | ||||
|  | ||||
|     JedisPoolConfig config = new JedisPoolConfig(); | ||||
|     if (redis.getMaxActive() != null) { | ||||
|  | ||||
| @ -0,0 +1,22 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.enums; | ||||
|  | ||||
| /** | ||||
|  * httpclient类型. | ||||
|  * | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  * @date 2020-08-30 | ||||
|  */ | ||||
| public enum HttpClientType { | ||||
|   /** | ||||
|    * HttpClient. | ||||
|    */ | ||||
|   HttpClient, | ||||
|   /** | ||||
|    * OkHttp. | ||||
|    */ | ||||
|   OkHttp, | ||||
|   /** | ||||
|    * JoddHttp. | ||||
|    */ | ||||
|   JoddHttp, | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.enums; | ||||
|  | ||||
| /** | ||||
|  * storage类型. | ||||
|  * | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  * @date 2020-08-30 | ||||
|  */ | ||||
| public enum StorageType { | ||||
|   /** | ||||
|    * 内存. | ||||
|    */ | ||||
|   Memory, | ||||
|   /** | ||||
|    * redis(JedisClient). | ||||
|    */ | ||||
|   Jedis, | ||||
|   /** | ||||
|    * redis(RedisTemplate). | ||||
|    */ | ||||
|   RedisTemplate | ||||
| } | ||||
| @ -0,0 +1,46 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.properties; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| import java.io.Serializable; | ||||
|  | ||||
| /** | ||||
|  * redis 配置属性. | ||||
|  * | ||||
|  * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
|  * @date 2020-08-30 | ||||
|  */ | ||||
| @Data | ||||
| public class RedisProperties implements Serializable { | ||||
|   private static final long serialVersionUID = -5924815351660074401L; | ||||
|  | ||||
|   /** | ||||
|    * 主机地址. | ||||
|    */ | ||||
|   private String host = "127.0.0.1"; | ||||
|  | ||||
|   /** | ||||
|    * 端口号. | ||||
|    */ | ||||
|   private int port = 6379; | ||||
|  | ||||
|   /** | ||||
|    * 密码. | ||||
|    */ | ||||
|   private String password; | ||||
|  | ||||
|   /** | ||||
|    * 超时. | ||||
|    */ | ||||
|   private int timeout = 2000; | ||||
|  | ||||
|   /** | ||||
|    * 数据库. | ||||
|    */ | ||||
|   private int database = 0; | ||||
|  | ||||
|   private Integer maxActive; | ||||
|   private Integer maxIdle; | ||||
|   private Integer maxWaitMillis; | ||||
|   private Integer minIdle; | ||||
| } | ||||
| @ -1,12 +1,14 @@ | ||||
| package com.binarywang.spring.starter.wxjava.mp.properties; | ||||
|  | ||||
| import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType; | ||||
| import com.binarywang.spring.starter.wxjava.mp.enums.StorageType; | ||||
| import lombok.Data; | ||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
|  | ||||
| import java.io.Serializable; | ||||
|  | ||||
| import static com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties.PREFIX; | ||||
| import static com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties.StorageType.memory; | ||||
| import static com.binarywang.spring.starter.wxjava.mp.enums.StorageType.Memory; | ||||
|  | ||||
|  | ||||
| /** | ||||
| @ -51,7 +53,7 @@ public class WxMpProperties { | ||||
|     /** | ||||
|      * 存储类型. | ||||
|      */ | ||||
|     private StorageType type = memory; | ||||
|     private StorageType type = Memory; | ||||
|  | ||||
|     /** | ||||
|      * 指定key前缀. | ||||
| @ -90,73 +92,4 @@ public class WxMpProperties { | ||||
|  | ||||
|   } | ||||
|  | ||||
|   public enum StorageType { | ||||
|     /** | ||||
|      * 内存. | ||||
|      */ | ||||
|     memory, | ||||
|     /** | ||||
|      * jedis. | ||||
|      */ | ||||
|     redis, | ||||
|     /** | ||||
|      * redis(JedisClient). | ||||
|      */ | ||||
|     jedis, | ||||
|     /** | ||||
|      * redis(RedisTemplate). | ||||
|      */ | ||||
|     redistemplate | ||||
|   } | ||||
|  | ||||
|   public enum HttpClientType { | ||||
|     /** | ||||
|      * HttpClient. | ||||
|      */ | ||||
|     httpclient, | ||||
|     /** | ||||
|      * OkHttp. | ||||
|      */ | ||||
|     okhttp, | ||||
|     /** | ||||
|      * JoddHttp. | ||||
|      */ | ||||
|     joddhttp | ||||
|   } | ||||
|  | ||||
|   @Data | ||||
|   public static class RedisProperties implements Serializable { | ||||
|     private static final long serialVersionUID = -5924815351660074401L; | ||||
|  | ||||
|     /** | ||||
|      * 主机地址. | ||||
|      */ | ||||
|     private String host = "127.0.0.1"; | ||||
|  | ||||
|     /** | ||||
|      * 端口号. | ||||
|      */ | ||||
|     private int port = 6379; | ||||
|  | ||||
|     /** | ||||
|      * 密码. | ||||
|      */ | ||||
|     private String password; | ||||
|  | ||||
|     /** | ||||
|      * 超时. | ||||
|      */ | ||||
|     private int timeout = 2000; | ||||
|  | ||||
|     /** | ||||
|      * 数据库. | ||||
|      */ | ||||
|     private int database = 0; | ||||
|  | ||||
|     private Integer maxActive; | ||||
|     private Integer maxIdle; | ||||
|     private Integer maxWaitMillis; | ||||
|     private Integer minIdle; | ||||
|   } | ||||
|  | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang