@@ -21,6 +21,7 @@ | |||||
<module>wx-java-miniapp-spring-boot-starter</module> | <module>wx-java-miniapp-spring-boot-starter</module> | ||||
<module>wx-java-mp-spring-boot-starter</module> | <module>wx-java-mp-spring-boot-starter</module> | ||||
<module>wx-java-pay-spring-boot-starter</module> | <module>wx-java-pay-spring-boot-starter</module> | ||||
<module>wx-java-open-spring-boot-starter</module> | |||||
</modules> | </modules> | ||||
<dependencies> | <dependencies> | ||||
@@ -0,0 +1,34 @@ | |||||
# wx-java-open-spring-boot-starter | |||||
## 快速开始 | |||||
1. 引入依赖 | |||||
```xml | |||||
<dependency> | |||||
<groupId>com.github.binarywang</groupId> | |||||
<artifactId>wx-java-open-spring-boot-starter</artifactId> | |||||
<version>${version}</version> | |||||
</dependency> | |||||
``` | |||||
2. 添加配置(application.properties) | |||||
``` | |||||
# 开放平台配置(必填) | |||||
wx.open.appId = @appId | |||||
wx.open.secret = @secret | |||||
wx.open.token = @token | |||||
wx.open.aesKey = @aesKey | |||||
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool | |||||
wx.open.config-storage.type = redis # 可选值, memory(默认), redis | |||||
wx.open.config-storage.redis.host = 127.0.0.1 | |||||
wx.open.config-storage.redis.port = 6379 | |||||
``` | |||||
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService` | |||||
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的 | |||||
- WxOpenConfigStorage | |||||
- WxOpenService | |||||
@@ -0,0 +1,73 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" | |||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<parent> | |||||
<groupId>com.github.binarywang</groupId> | |||||
<artifactId>wx-java</artifactId> | |||||
<version>3.5.2.B</version> | |||||
<relativePath>../../</relativePath> | |||||
</parent> | |||||
<artifactId>wx-java-open-spring-boot-starter</artifactId> | |||||
<name>WxJava - Spring Boot Starter for OEPN</name> | |||||
<description>微信开放平台开发的 Spring Boot Starter</description> | |||||
<properties> | |||||
<spring.boot.version>2.1.4.RELEASE</spring.boot.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-autoconfigure</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-configuration-processor</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.github.binarywang</groupId> | |||||
<artifactId>weixin-java-open</artifactId> | |||||
<version>${project.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>redis.clients</groupId> | |||||
<artifactId>jedis</artifactId> | |||||
<scope>compile</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<scope>provided</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
</plugin> | |||||
<plugin> | |||||
<groupId>org.apache.maven.plugins</groupId> | |||||
<artifactId>maven-source-plugin</artifactId> | |||||
<version>2.2.1</version> | |||||
<executions> | |||||
<execution> | |||||
<id>attach-sources</id> | |||||
<goals> | |||||
<goal>jar-no-fork</goal> | |||||
</goals> | |||||
</execution> | |||||
</executions> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,17 @@ | |||||
package com.binarywang.spring.starter.wxjava.open.config; | |||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; | |||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.context.annotation.Import; | |||||
/** | |||||
* . | |||||
* | |||||
* @author someone | |||||
*/ | |||||
@Configuration | |||||
@EnableConfigurationProperties(WxOpenProperties.class) | |||||
@Import({WxOpenStorageAutoConfiguration.class, WxOpenServiceAutoConfiguration.class}) | |||||
public class WxOpenAutoConfiguration { | |||||
} |
@@ -0,0 +1,39 @@ | |||||
package com.binarywang.spring.starter.wxjava.open.config; | |||||
import me.chanjar.weixin.open.api.WxOpenComponentService; | |||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage; | |||||
import me.chanjar.weixin.open.api.WxOpenService; | |||||
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter; | |||||
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl; | |||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
/** | |||||
* 微信开放平台相关服务自动注册. | |||||
* | |||||
* @author someone | |||||
*/ | |||||
@Configuration | |||||
public class WxOpenServiceAutoConfiguration { | |||||
@Bean | |||||
@ConditionalOnMissingBean | |||||
public WxOpenService wxOpenService(WxOpenConfigStorage configStorage) { | |||||
WxOpenService wxOpenService = new WxOpenServiceImpl(); | |||||
wxOpenService.setWxOpenConfigStorage(configStorage); | |||||
return wxOpenService; | |||||
} | |||||
@Bean | |||||
public WxOpenMessageRouter wxOpenMessageRouter(WxOpenService wxOpenService) { | |||||
return new WxOpenMessageRouter(wxOpenService); | |||||
} | |||||
@Bean | |||||
public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) { | |||||
return wxOpenService.getWxOpenComponentService(); | |||||
} | |||||
} |
@@ -0,0 +1,93 @@ | |||||
package com.binarywang.spring.starter.wxjava.open.config; | |||||
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties; | |||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; | |||||
import lombok.RequiredArgsConstructor; | |||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage; | |||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; | |||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import redis.clients.jedis.JedisPool; | |||||
import redis.clients.jedis.JedisPoolConfig; | |||||
/** | |||||
* 微信公众号存储策略自动配置. | |||||
* | |||||
* @author someone | |||||
*/ | |||||
@Configuration | |||||
@RequiredArgsConstructor | |||||
public class WxOpenStorageAutoConfiguration { | |||||
private final WxOpenProperties properties; | |||||
@Autowired(required = false) | |||||
private JedisPool jedisPool; | |||||
@Value("${wx.open.config-storage.redis.host:}") | |||||
private String redisHost; | |||||
@Bean | |||||
@ConditionalOnMissingBean(WxOpenConfigStorage.class) | |||||
public WxOpenConfigStorage wxOpenConfigStorage() { | |||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); | |||||
WxOpenProperties.StorageType type = storage.getType(); | |||||
if (type == WxOpenProperties.StorageType.redis) { | |||||
return getWxOpenInRedisConfigStorage(); | |||||
} | |||||
return getWxOpenInMemoryConfigStorage(); | |||||
} | |||||
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() { | |||||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage(); | |||||
setWxOpenInfo(config); | |||||
return config; | |||||
} | |||||
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() { | |||||
JedisPool poolToUse = jedisPool; | |||||
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) { | |||||
poolToUse = getJedisPool(); | |||||
} | |||||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse); | |||||
setWxOpenInfo(config); | |||||
return config; | |||||
} | |||||
private void setWxOpenInfo(WxOpenConfigStorage config) { | |||||
config.setComponentAppId(properties.getAppId()); | |||||
config.setComponentAppSecret(properties.getSecret()); | |||||
config.setComponentToken(properties.getToken()); | |||||
config.setComponentAesKey(properties.getAesKey()); | |||||
} | |||||
private JedisPool getJedisPool() { | |||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); | |||||
RedisProperties 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); | |||||
JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), | |||||
redis.getTimeout(), redis.getPassword(), redis.getDatabase()); | |||||
return pool; | |||||
} | |||||
} |
@@ -0,0 +1,45 @@ | |||||
package com.binarywang.spring.starter.wxjava.open.properties; | |||||
import lombok.Data; | |||||
import java.io.Serializable; | |||||
/** | |||||
* Redis配置. | |||||
* | |||||
* @author someone | |||||
*/ | |||||
@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; | |||||
} |
@@ -0,0 +1,68 @@ | |||||
package com.binarywang.spring.starter.wxjava.open.properties; | |||||
import lombok.Data; | |||||
import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
import java.io.Serializable; | |||||
import static com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties.PREFIX; | |||||
import static com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties.StorageType.memory; | |||||
/** | |||||
* 微信接入相关配置属性. | |||||
* | |||||
* @author someone | |||||
*/ | |||||
@Data | |||||
@ConfigurationProperties(PREFIX) | |||||
public class WxOpenProperties { | |||||
public static final String PREFIX = "wx.open"; | |||||
/** | |||||
* 设置微信开放平台的appid. | |||||
*/ | |||||
private String appId; | |||||
/** | |||||
* 设置微信开放平台的app secret. | |||||
*/ | |||||
private String secret; | |||||
/** | |||||
* 设置微信开放平台的token. | |||||
*/ | |||||
private String token; | |||||
/** | |||||
* 设置微信开放平台的EncodingAESKey. | |||||
*/ | |||||
private String aesKey; | |||||
/** | |||||
* 存储策略, memory, redis. | |||||
*/ | |||||
private ConfigStorage configStorage = new ConfigStorage(); | |||||
@Data | |||||
public static class ConfigStorage implements Serializable { | |||||
private static final long serialVersionUID = 4815731027000065434L; | |||||
private StorageType type = memory; | |||||
private RedisProperties redis = new RedisProperties(); | |||||
} | |||||
public enum StorageType { | |||||
/** | |||||
* 内存. | |||||
*/ | |||||
memory, | |||||
/** | |||||
* redis. | |||||
*/ | |||||
redis | |||||
} | |||||
} |
@@ -0,0 +1 @@ | |||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.binarywang.spring.starter.wxjava.open.config.WxOpenAutoConfiguration |