mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-07-17 19:37:36 +08:00
🎨 小程序 Spring Boot Starter 模块优化重构
This commit is contained in:
parent
8bb6b02cba
commit
7b641e7b5b
@ -18,12 +18,12 @@
|
|||||||
wx.miniapp.msgDataFormat = @msgDataFormat # 消息格式,XML或者JSON.
|
wx.miniapp.msgDataFormat = @msgDataFormat # 消息格式,XML或者JSON.
|
||||||
# 存储配置redis(可选)
|
# 存储配置redis(可选)
|
||||||
# 注意: 指定redis.host值后不会使用容器注入的redis连接(JedisPool)
|
# 注意: 指定redis.host值后不会使用容器注入的redis连接(JedisPool)
|
||||||
wx.miniapp.config-storage.type = jedis # 配置类型: memory(默认), jedis, redistemplate
|
wx.miniapp.config-storage.type = Jedis # 配置类型: Memory(默认), Jedis, RedisTemplate
|
||||||
wx.miniapp.config-storage.key-prefix = wa # 相关redis前缀配置: wa(默认)
|
wx.miniapp.config-storage.key-prefix = wa # 相关redis前缀配置: wa(默认)
|
||||||
wx.miniapp.config-storage.redis.host = 127.0.0.1
|
wx.miniapp.config-storage.redis.host = 127.0.0.1
|
||||||
wx.miniapp.config-storage.redis.port = 6379
|
wx.miniapp.config-storage.redis.port = 6379
|
||||||
# http客户端配置
|
# http客户端配置
|
||||||
wx.miniapp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
|
wx.miniapp.config-storage.http-client-type=HttpClient # http客户端类型: HttpClient(默认)
|
||||||
wx.miniapp.config-storage.http-proxy-host=
|
wx.miniapp.config-storage.http-proxy-host=
|
||||||
wx.miniapp.config-storage.http-proxy-port=
|
wx.miniapp.config-storage.http-proxy-port=
|
||||||
wx.miniapp.config-storage.http-proxy-username=
|
wx.miniapp.config-storage.http-proxy-username=
|
||||||
|
@ -22,13 +22,11 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>redis.clients</groupId>
|
<groupId>redis.clients</groupId>
|
||||||
<artifactId>jedis</artifactId>
|
<artifactId>jedis</artifactId>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.data</groupId>
|
<groupId>org.springframework.data</groupId>
|
||||||
<artifactId>spring-data-redis</artifactId>
|
<artifactId>spring-data-redis</artifactId>
|
||||||
<version>${spring.boot.version}</version>
|
<version>${spring.boot.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -5,6 +5,9 @@ import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
|||||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||||
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
|
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
|
||||||
|
import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType;
|
||||||
|
import com.binarywang.spring.starter.wxjava.miniapp.properties.ConfigStorage;
|
||||||
|
import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties;
|
||||||
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
|
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||||
@ -54,14 +57,17 @@ public class WxMaAutoConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(WxMaConfig.class)
|
@ConditionalOnMissingBean(WxMaConfig.class)
|
||||||
public WxMaConfig wxMaConfig() {
|
public WxMaConfig wxMaConfig() {
|
||||||
WxMaProperties.StorageType type = wxMaProperties.getConfigStorage().getType();
|
|
||||||
WxMaDefaultConfigImpl config;
|
WxMaDefaultConfigImpl config;
|
||||||
if (type == WxMaProperties.StorageType.jedis) {
|
switch (wxMaProperties.getConfigStorage().getType()) {
|
||||||
config = wxMaInJedisConfigStorage();
|
case Jedis:
|
||||||
} else if (type == WxMaProperties.StorageType.redistemplate) {
|
config = wxMaJedisConfigStorage();
|
||||||
config = wxMaInRedisTemplateConfigStorage();
|
break;
|
||||||
} else {
|
case RedisTemplate:
|
||||||
config = wxMaInMemoryConfigStorage();
|
config = wxMaRedisTemplateConfigStorage();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
config = wxMaDefaultConfigStorage();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
config.setAppid(StringUtils.trimToNull(this.wxMaProperties.getAppid()));
|
config.setAppid(StringUtils.trimToNull(this.wxMaProperties.getAppid()));
|
||||||
@ -70,7 +76,7 @@ public class WxMaAutoConfiguration {
|
|||||||
config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey()));
|
config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey()));
|
||||||
config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat()));
|
config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat()));
|
||||||
|
|
||||||
WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
|
ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
|
||||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||||
@ -80,13 +86,12 @@ public class WxMaAutoConfiguration {
|
|||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMaDefaultConfigImpl wxMaInMemoryConfigStorage() {
|
private WxMaDefaultConfigImpl wxMaDefaultConfigStorage() {
|
||||||
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
return new WxMaDefaultConfigImpl();
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMaDefaultConfigImpl wxMaInJedisConfigStorage() {
|
private WxMaDefaultConfigImpl wxMaJedisConfigStorage() {
|
||||||
WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
|
RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
|
||||||
JedisPool jedisPool;
|
JedisPool jedisPool;
|
||||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||||
jedisPool = getJedisPool();
|
jedisPool = getJedisPool();
|
||||||
@ -94,20 +99,18 @@ public class WxMaAutoConfiguration {
|
|||||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||||
}
|
}
|
||||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||||
WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
|
return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
|
||||||
return wxMaRedisConfig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WxMaDefaultConfigImpl wxMaInRedisTemplateConfigStorage() {
|
private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage() {
|
||||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||||
WxMaRedisBetterConfigImpl wxMaRedisConfig = new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
|
return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
|
||||||
return wxMaRedisConfig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private JedisPool getJedisPool() {
|
private JedisPool getJedisPool() {
|
||||||
WxMaProperties.ConfigStorage storage = wxMaProperties.getConfigStorage();
|
ConfigStorage storage = wxMaProperties.getConfigStorage();
|
||||||
WxMaProperties.RedisProperties redis = storage.getRedis();
|
RedisProperties redis = storage.getRedis();
|
||||||
|
|
||||||
JedisPoolConfig config = new JedisPoolConfig();
|
JedisPoolConfig config = new JedisPoolConfig();
|
||||||
if (redis.getMaxActive() != null) {
|
if (redis.getMaxActive() != null) {
|
||||||
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.binarywang.spring.starter.wxjava.miniapp.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* httpclient类型.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-05-25
|
||||||
|
*/
|
||||||
|
public enum HttpClientType {
|
||||||
|
/**
|
||||||
|
* HttpClient.
|
||||||
|
*/
|
||||||
|
HttpClient
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.binarywang.spring.starter.wxjava.miniapp.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* storage类型.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-05-25
|
||||||
|
*/
|
||||||
|
public enum StorageType {
|
||||||
|
/**
|
||||||
|
* 内存.
|
||||||
|
*/
|
||||||
|
Memory,
|
||||||
|
/**
|
||||||
|
* redis(JedisClient).
|
||||||
|
*/
|
||||||
|
Jedis,
|
||||||
|
/**
|
||||||
|
* redis(RedisTemplate).
|
||||||
|
*/
|
||||||
|
RedisTemplate
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.binarywang.spring.starter.wxjava.miniapp.properties;
|
||||||
|
|
||||||
|
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
|
||||||
|
import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储策略.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-05-25
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class ConfigStorage implements Serializable {
|
||||||
|
private static final long serialVersionUID = 4815731027000065434L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储类型.
|
||||||
|
*/
|
||||||
|
private StorageType type = StorageType.Memory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定key前缀.
|
||||||
|
*/
|
||||||
|
private String keyPrefix = "wa";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redis连接配置.
|
||||||
|
*/
|
||||||
|
private RedisProperties redis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http客户端类型.
|
||||||
|
*/
|
||||||
|
private HttpClientType httpClientType = HttpClientType.HttpClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http代理主机.
|
||||||
|
*/
|
||||||
|
private String httpProxyHost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http代理端口.
|
||||||
|
*/
|
||||||
|
private Integer httpProxyPort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http代理用户名.
|
||||||
|
*/
|
||||||
|
private String httpProxyUsername;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http代理密码.
|
||||||
|
*/
|
||||||
|
private String httpProxyPassword;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.binarywang.spring.starter.wxjava.miniapp.properties;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redis配置.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-05-25
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class RedisProperties implements Serializable {
|
||||||
|
private static final long serialVersionUID = -5924815351660074401L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主机地址.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
@ -3,8 +3,6 @@ package com.binarywang.spring.starter.wxjava.miniapp.properties;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 属性配置类.
|
* 属性配置类.
|
||||||
*
|
*
|
||||||
@ -44,106 +42,4 @@ public class WxMaProperties {
|
|||||||
*/
|
*/
|
||||||
private ConfigStorage configStorage = new ConfigStorage();
|
private ConfigStorage configStorage = new ConfigStorage();
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class ConfigStorage implements Serializable {
|
|
||||||
private static final long serialVersionUID = 4815731027000065434L;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储类型.
|
|
||||||
*/
|
|
||||||
private StorageType type = StorageType.memory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 指定key前缀.
|
|
||||||
*/
|
|
||||||
private String keyPrefix = "wa";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* redis连接配置.
|
|
||||||
*/
|
|
||||||
private RedisProperties redis;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* http客户端类型.
|
|
||||||
*/
|
|
||||||
private HttpClientType httpClientType = HttpClientType.httpclient;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* http代理主机.
|
|
||||||
*/
|
|
||||||
private String httpProxyHost;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* http代理端口.
|
|
||||||
*/
|
|
||||||
private Integer httpProxyPort;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* http代理用户名.
|
|
||||||
*/
|
|
||||||
private String httpProxyUsername;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* http代理密码.
|
|
||||||
*/
|
|
||||||
private String httpProxyPassword;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum StorageType {
|
|
||||||
/**
|
|
||||||
* 内存.
|
|
||||||
*/
|
|
||||||
memory,
|
|
||||||
/**
|
|
||||||
* redis(JedisClient).
|
|
||||||
*/
|
|
||||||
jedis,
|
|
||||||
/**
|
|
||||||
* redis(RedisTemplate).
|
|
||||||
*/
|
|
||||||
redistemplate
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum HttpClientType {
|
|
||||||
/**
|
|
||||||
* HttpClient.
|
|
||||||
*/
|
|
||||||
httpclient
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class RedisProperties implements Serializable {
|
|
||||||
private static final long serialVersionUID = -5924815351660074401L;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 主机地址.
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user