Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

公众号spring-boot-starter支持前缀配置, RedisTemplate, http客户端配置 #1516

Merged
merged 7 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions spring-boot-starters/wx-java-mp-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
2. 添加配置(application.properties)
```properties
# 公众号配置(必填)
wx.mp.appId = @appId
wx.mp.secret = @secret
wx.mp.token = @token
wx.mp.aesKey = @aesKey
# 存储配置redis(可选)
wx.mp.config-storage.type = redis
wx.mp.config-storage.redis.host = 127.0.0.1
wx.mp.config-storage.redis.port = 6379
wx.mp.appId = appId
wx.mp.secret = @secret
wx.mp.token = @token
wx.mp.aesKey = @aesKey
# 存储配置redis(可选)
wx.mp.config-storage.type = redis # 配置类型: memory(默认), redis, 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-proxy-host=
wx.mp.config-storage.http-proxy-port=
wx.mp.config-storage.http-proxy-username=
wx.mp.config-storage.http-proxy-password=
```
3. 支持自动注入的类型

`WxMpService`以及相关的服务类, 比如: `wxMpService.getXxxService`。
`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~



Expand Down
8 changes: 4 additions & 4 deletions spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>compile</scope>
<optional>true</optional>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.binarywang.spring.starter.wxjava.mp.config;

import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
Expand All @@ -17,113 +21,161 @@ public class WxMpServiceAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public WxMpService wxMpService(WxMpConfigStorage configStorage) {
WxMpService wxMpService = new WxMpServiceImpl();
public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) {
WxMpProperties.HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
WxMpService wxMpService;
if (httpClientType == WxMpProperties.HttpClientType.okhttp) {
wxMpService = newWxMpServiceJoddHttpImpl();
} else if (httpClientType == WxMpProperties.HttpClientType.joddhttp) {
wxMpService = newWxMpServiceOkHttpImpl();
} else if (httpClientType == WxMpProperties.HttpClientType.httpclient) {
wxMpService = newWxMpServiceHttpClientImpl();
} else {
wxMpService = newWxMpServiceImpl();
}

wxMpService.setWxMpConfigStorage(configStorage);
return wxMpService;
}

private WxMpService newWxMpServiceImpl() {
return new WxMpServiceImpl();
}

private WxMpService newWxMpServiceHttpClientImpl() {
return new WxMpServiceHttpClientImpl();
}

private WxMpService newWxMpServiceOkHttpImpl() {
return new WxMpServiceOkHttpImpl();
}

private WxMpService newWxMpServiceJoddHttpImpl() {
return new WxMpServiceJoddHttpImpl();
}

@Bean
@Deprecated
public WxMpKefuService wxMpKefuService(WxMpService wxMpService) {
return wxMpService.getKefuService();
}

@Bean
@Deprecated
public WxMpMaterialService wxMpMaterialService(WxMpService wxMpService) {
return wxMpService.getMaterialService();
}

@Bean
@Deprecated
public WxMpMenuService wxMpMenuService(WxMpService wxMpService) {
return wxMpService.getMenuService();
}

@Bean
@Deprecated
public WxMpUserService wxMpUserService(WxMpService wxMpService) {
return wxMpService.getUserService();
}

@Bean
@Deprecated
public WxMpUserTagService wxMpUserTagService(WxMpService wxMpService) {
return wxMpService.getUserTagService();
}

@Bean
@Deprecated
public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) {
return wxMpService.getQrcodeService();
}

@Bean
@Deprecated
public WxMpCardService wxMpCardService(WxMpService wxMpService) {
return wxMpService.getCardService();
}

@Bean
@Deprecated
public WxMpDataCubeService wxMpDataCubeService(WxMpService wxMpService) {
return wxMpService.getDataCubeService();
}

@Bean
@Deprecated
public WxMpUserBlacklistService wxMpUserBlacklistService(WxMpService wxMpService) {
return wxMpService.getBlackListService();
}

@Bean
@Deprecated
public WxMpStoreService wxMpStoreService(WxMpService wxMpService) {
return wxMpService.getStoreService();
}

@Bean
@Deprecated
public WxMpTemplateMsgService wxMpTemplateMsgService(WxMpService wxMpService) {
return wxMpService.getTemplateMsgService();
}

@Bean
@Deprecated
public WxMpSubscribeMsgService wxMpSubscribeMsgService(WxMpService wxMpService) {
return wxMpService.getSubscribeMsgService();
}

@Bean
@Deprecated
public WxMpDeviceService wxMpDeviceService(WxMpService wxMpService) {
return wxMpService.getDeviceService();
}

@Bean
@Deprecated
public WxMpShakeService wxMpShakeService(WxMpService wxMpService) {
return wxMpService.getShakeService();
}

@Bean
@Deprecated
public WxMpMemberCardService wxMpMemberCardService(WxMpService wxMpService) {
return wxMpService.getMemberCardService();
}

@Bean
@Deprecated
public WxMpMassMessageService wxMpMassMessageService(WxMpService wxMpService) {
return wxMpService.getMassMessageService();
}

@Bean
@Deprecated
public WxMpAiOpenService wxMpAiOpenService(WxMpService wxMpService) {
return wxMpService.getAiOpenService();
}

@Bean
@Deprecated
public WxMpWifiService wxMpWifiService(WxMpService wxMpService) {
return wxMpService.getWifiService();
}

@Bean
@Deprecated
public WxMpMarketingService wxMpMarketingService(WxMpService wxMpService) {
return wxMpService.getMarketingService();
}

@Bean
@Deprecated
public WxMpCommentService wxMpCommentService(WxMpService wxMpService) {
return wxMpService.getCommentService();
}

@Bean
@Deprecated
public WxMpOcrService wxMpOcrService(WxMpService wxMpService) {
return wxMpService.getOcrService();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.binarywang.spring.starter.wxjava.mp.config;

import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.mp.extend.RedisTemplateWxMpRedisOps;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import me.chanjar.weixin.mp.config.redis.JedisWxMpRedisOps;
import me.chanjar.weixin.mp.config.redis.WxMpRedisOps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
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;

Expand All @@ -22,55 +27,77 @@
@RequiredArgsConstructor
public class WxMpStorageAutoConfiguration {

private final WxMpProperties properties;
private final ApplicationContext applicationContext;

private final WxMpProperties wxMpProperties;

@Value("${wx.mp.config-storage.redis.host:")
private String redisHost;

@Value("${wx.mp.configStorage.redis.host:")
private String redisHost2;

@Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class)
public WxMpConfigStorage wxMpInMemoryConfigStorage() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
WxMpProperties.StorageType type = storage.getType();

if (type == WxMpProperties.StorageType.redis) {
return getWxMpInRedisConfigStorage();
public WxMpConfigStorage wxMpConfigStorage() {
WxMpProperties.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();
}
return getWxMpInMemoryConfigStorage();
return config;
}

private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() {
private WxMpConfigStorage wxMpInMemoryConfigStorage() {
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
setWxMpInfo(config);
return config;
}

private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() {
RedisProperties.ImplType implType = properties.getConfigStorage().getRedis().getImpl();
boolean reuseBean = properties.getConfigStorage().getRedis().isReuseBean();
if (implType == RedisProperties.ImplType.jedis) {
JedisPool pool = null;
if (reuseBean) {
pool = getBean(JedisPool.class);
}
if (pool == null) {
pool = getJedisPool();
}
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(pool);
setWxMpInfo(config);
return config;
private WxMpConfigStorage wxMpInJedisConfigStorage() {
JedisPool jedisPool;
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
jedisPool = getJedisPool();
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
throw new UnsupportedOperationException();
WxMpRedisOps redisOps = new JedisWxMpRedisOps(jedisPool);
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
}

private WxMpConfigStorage wxMpInRedisTemplateConfigStorage() {
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
WxMpRedisOps redisOps = new RedisTemplateWxMpRedisOps(redisTemplate);
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
}

private void setWxMpInfo(WxMpDefaultConfigImpl config) {
WxMpProperties properties = wxMpProperties;
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setAppId(properties.getAppId());
config.setSecret(properties.getSecret());
config.setToken(properties.getToken());
config.setAesKey(properties.getAesKey());

config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
}

private JedisPool getJedisPool() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
RedisProperties redis = storage.getRedis();
WxMpProperties.ConfigStorage storage = wxMpProperties.getConfigStorage();
WxMpProperties.RedisProperties redis = storage.getRedis();

JedisPoolConfig config = new JedisPoolConfig();
if (redis.getMaxActive() != null) {
Expand All @@ -91,11 +118,4 @@ private JedisPool getJedisPool() {
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
redis.getDatabase());
}

private <T> T getBean(Class<T> clazz) {
if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
return this.applicationContext.getBean(clazz);
}
return null;
}
}
Loading