mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🎨 优化重构并统一公众号和小程序的spring boot starter部分配置类和属性
This commit is contained in:
parent
64f7adcc29
commit
7cfabab628
@ -9,6 +9,7 @@ import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
|
||||
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
|
||||
import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
@ -52,14 +53,19 @@ public class WxMaAutoConfiguration {
|
||||
public WxMaService service(WxMaConfig wxMaConfig) {
|
||||
HttpClientType httpClientType = wxMaProperties.getConfigStorage().getHttpClientType();
|
||||
WxMaService wxMaService;
|
||||
if (httpClientType == HttpClientType.OkHttp) {
|
||||
wxMaService = new WxMaServiceOkHttpImpl();
|
||||
} else if (httpClientType == HttpClientType.JoddHttp) {
|
||||
wxMaService = new WxMaServiceJoddHttpImpl();
|
||||
} else if (httpClientType == HttpClientType.HttpClient) {
|
||||
wxMaService = new WxMaServiceHttpClientImpl();
|
||||
} else {
|
||||
wxMaService = new WxMaServiceImpl();
|
||||
switch (httpClientType) {
|
||||
case OkHttp:
|
||||
wxMaService = new WxMaServiceOkHttpImpl();
|
||||
break;
|
||||
case JoddHttp:
|
||||
wxMaService = new WxMaServiceJoddHttpImpl();
|
||||
break;
|
||||
case HttpClient:
|
||||
wxMaService = new WxMaServiceHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
wxMaService = new WxMaServiceImpl();
|
||||
break;
|
||||
}
|
||||
wxMaService.setWxMaConfig(wxMaConfig);
|
||||
return wxMaService;
|
||||
@ -102,7 +108,7 @@ public class WxMaAutoConfiguration {
|
||||
}
|
||||
|
||||
private WxMaDefaultConfigImpl wxMaJedisConfigStorage() {
|
||||
WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
|
||||
RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
|
@ -0,0 +1,43 @@
|
||||
package com.binarywang.spring.starter.wxjava.miniapp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* redis 配置.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-08-30
|
||||
*/
|
||||
@Data
|
||||
public class RedisProperties {
|
||||
|
||||
/**
|
||||
* 主机地址.不填则从spring容器内获取JedisPool
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
*/
|
||||
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;
|
||||
}
|
@ -88,37 +88,4 @@ public class WxMaProperties {
|
||||
private String httpProxyPassword;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class RedisProperties {
|
||||
|
||||
/**
|
||||
* 主机地址.不填则从spring容器内获取JedisPool
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,12 @@
|
||||
wx.mp.token = @token
|
||||
wx.mp.aesKey = @aesKey
|
||||
# 存储配置redis(可选)
|
||||
wx.mp.config-storage.type = redis # 配置类型: memory(默认), redis, jedis, redistemplate
|
||||
wx.mp.config-storage.type = Jedis # 配置类型: Memory(默认), Jedis, RedisTemplate
|
||||
wx.mp.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
|
||||
wx.mp.config-storage.redis.host = 127.0.0.1
|
||||
wx.mp.config-storage.redis.port = 6379
|
||||
# http客户端配置
|
||||
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认), okhttp, joddhttp
|
||||
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
|
||||
wx.mp.config-storage.http-proxy-host=
|
||||
wx.mp.config-storage.http-proxy-port=
|
||||
wx.mp.config-storage.http-proxy-username=
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user