Skip to content

Commit 73286d4

Browse files
lkqmbinarywang
authored andcommitted
#1083 增加微信开放平台模块的spring-boot-starter
1 parent 8e97b77 commit 73286d4

File tree

9 files changed

+371
-0
lines changed

9 files changed

+371
-0
lines changed

spring-boot-starters/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<module>wx-java-miniapp-spring-boot-starter</module>
2222
<module>wx-java-mp-spring-boot-starter</module>
2323
<module>wx-java-pay-spring-boot-starter</module>
24+
<module>wx-java-open-spring-boot-starter</module>
2425
</modules>
2526

2627
<dependencies>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# wx-java-open-spring-boot-starter
2+
## 快速开始
3+
1. 引入依赖
4+
```xml
5+
<dependency>
6+
<groupId>com.github.binarywang</groupId>
7+
<artifactId>wx-java-open-spring-boot-starter</artifactId>
8+
<version>${version}</version>
9+
</dependency>
10+
```
11+
2. 添加配置(application.properties)
12+
```
13+
# 开放平台配置(必填)
14+
wx.open.appId = @appId
15+
wx.open.secret = @secret
16+
wx.open.token = @token
17+
wx.open.aesKey = @aesKey
18+
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool
19+
wx.open.config-storage.type = redis # 可选值, memory(默认), redis
20+
wx.open.config-storage.redis.host = 127.0.0.1
21+
wx.open.config-storage.redis.port = 6379
22+
```
23+
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`
24+
25+
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
26+
- WxOpenConfigStorage
27+
- WxOpenService
28+
29+
30+
31+
32+
33+
34+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.github.binarywang</groupId>
8+
<artifactId>wx-java</artifactId>
9+
<version>3.5.2.B</version>
10+
<relativePath>../../</relativePath>
11+
</parent>
12+
13+
<artifactId>wx-java-open-spring-boot-starter</artifactId>
14+
<name>WxJava - Spring Boot Starter for OEPN</name>
15+
<description>微信开放平台开发的 Spring Boot Starter</description>
16+
17+
<properties>
18+
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-autoconfigure</artifactId>
25+
<version>${spring.boot.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-configuration-processor</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<optional>true</optional>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.github.binarywang</groupId>
35+
<artifactId>weixin-java-open</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>redis.clients</groupId>
40+
<artifactId>jedis</artifactId>
41+
<scope>compile</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<scope>provided</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
<version>${spring.boot.version}</version>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-source-plugin</artifactId>
60+
<version>2.2.1</version>
61+
<executions>
62+
<execution>
63+
<id>attach-sources</id>
64+
<goals>
65+
<goal>jar-no-fork</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.binarywang.spring.starter.wxjava.open.config;
2+
3+
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
4+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Import;
7+
8+
/**
9+
* .
10+
*
11+
* @author someone
12+
*/
13+
@Configuration
14+
@EnableConfigurationProperties(WxOpenProperties.class)
15+
@Import({WxOpenStorageAutoConfiguration.class, WxOpenServiceAutoConfiguration.class})
16+
public class WxOpenAutoConfiguration {
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.binarywang.spring.starter.wxjava.open.config;
2+
3+
import me.chanjar.weixin.open.api.WxOpenComponentService;
4+
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
5+
import me.chanjar.weixin.open.api.WxOpenService;
6+
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
7+
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
/**
13+
* 微信开放平台相关服务自动注册.
14+
*
15+
* @author someone
16+
*/
17+
@Configuration
18+
public class WxOpenServiceAutoConfiguration {
19+
20+
@Bean
21+
@ConditionalOnMissingBean
22+
public WxOpenService wxOpenService(WxOpenConfigStorage configStorage) {
23+
WxOpenService wxOpenService = new WxOpenServiceImpl();
24+
wxOpenService.setWxOpenConfigStorage(configStorage);
25+
return wxOpenService;
26+
}
27+
28+
@Bean
29+
public WxOpenMessageRouter wxOpenMessageRouter(WxOpenService wxOpenService) {
30+
return new WxOpenMessageRouter(wxOpenService);
31+
}
32+
33+
@Bean
34+
public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) {
35+
return wxOpenService.getWxOpenComponentService();
36+
}
37+
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.binarywang.spring.starter.wxjava.open.config;
2+
3+
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties;
4+
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
5+
import lombok.RequiredArgsConstructor;
6+
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
7+
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
8+
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
9+
import org.apache.commons.lang3.StringUtils;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import redis.clients.jedis.JedisPool;
16+
import redis.clients.jedis.JedisPoolConfig;
17+
18+
/**
19+
* 微信公众号存储策略自动配置.
20+
*
21+
* @author someone
22+
*/
23+
@Configuration
24+
@RequiredArgsConstructor
25+
public class WxOpenStorageAutoConfiguration {
26+
private final WxOpenProperties properties;
27+
28+
@Autowired(required = false)
29+
private JedisPool jedisPool;
30+
31+
@Value("${wx.open.config-storage.redis.host:}")
32+
private String redisHost;
33+
34+
@Bean
35+
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
36+
public WxOpenConfigStorage wxOpenConfigStorage() {
37+
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
38+
WxOpenProperties.StorageType type = storage.getType();
39+
40+
if (type == WxOpenProperties.StorageType.redis) {
41+
return getWxOpenInRedisConfigStorage();
42+
}
43+
return getWxOpenInMemoryConfigStorage();
44+
}
45+
46+
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
47+
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
48+
setWxOpenInfo(config);
49+
return config;
50+
}
51+
52+
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
53+
JedisPool poolToUse = jedisPool;
54+
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) {
55+
poolToUse = getJedisPool();
56+
}
57+
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse);
58+
setWxOpenInfo(config);
59+
return config;
60+
}
61+
62+
private void setWxOpenInfo(WxOpenConfigStorage config) {
63+
config.setComponentAppId(properties.getAppId());
64+
config.setComponentAppSecret(properties.getSecret());
65+
config.setComponentToken(properties.getToken());
66+
config.setComponentAesKey(properties.getAesKey());
67+
}
68+
69+
private JedisPool getJedisPool() {
70+
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
71+
RedisProperties redis = storage.getRedis();
72+
73+
JedisPoolConfig config = new JedisPoolConfig();
74+
if (redis.getMaxActive() != null) {
75+
config.setMaxTotal(redis.getMaxActive());
76+
}
77+
if (redis.getMaxIdle() != null) {
78+
config.setMaxIdle(redis.getMaxIdle());
79+
}
80+
if (redis.getMaxWaitMillis() != null) {
81+
config.setMaxWaitMillis(redis.getMaxWaitMillis());
82+
}
83+
if (redis.getMinIdle() != null) {
84+
config.setMinIdle(redis.getMinIdle());
85+
}
86+
config.setTestOnBorrow(true);
87+
config.setTestWhileIdle(true);
88+
89+
JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(),
90+
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
91+
return pool;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.binarywang.spring.starter.wxjava.open.properties;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* Redis配置.
9+
*
10+
* @author someone
11+
*/
12+
@Data
13+
public class RedisProperties implements Serializable {
14+
private static final long serialVersionUID = -5924815351660074401L;
15+
16+
/**
17+
* 主机地址.
18+
*/
19+
private String host = "127.0.0.1";
20+
21+
/**
22+
* 端口号.
23+
*/
24+
private int port = 6379;
25+
26+
/**
27+
* 密码.
28+
*/
29+
private String password;
30+
31+
/**
32+
* 超时.
33+
*/
34+
private int timeout = 2000;
35+
36+
/**
37+
* 数据库.
38+
*/
39+
private int database = 0;
40+
41+
private Integer maxActive;
42+
private Integer maxIdle;
43+
private Integer maxWaitMillis;
44+
private Integer minIdle;
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.binarywang.spring.starter.wxjava.open.properties;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
import java.io.Serializable;
7+
8+
import static com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties.PREFIX;
9+
import static com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties.StorageType.memory;
10+
11+
12+
/**
13+
* 微信接入相关配置属性.
14+
*
15+
* @author someone
16+
*/
17+
@Data
18+
@ConfigurationProperties(PREFIX)
19+
public class WxOpenProperties {
20+
public static final String PREFIX = "wx.open";
21+
22+
/**
23+
* 设置微信开放平台的appid.
24+
*/
25+
private String appId;
26+
27+
/**
28+
* 设置微信开放平台的app secret.
29+
*/
30+
private String secret;
31+
32+
/**
33+
* 设置微信开放平台的token.
34+
*/
35+
private String token;
36+
37+
/**
38+
* 设置微信开放平台的EncodingAESKey.
39+
*/
40+
private String aesKey;
41+
42+
/**
43+
* 存储策略, memory, redis.
44+
*/
45+
private ConfigStorage configStorage = new ConfigStorage();
46+
47+
48+
@Data
49+
public static class ConfigStorage implements Serializable {
50+
private static final long serialVersionUID = 4815731027000065434L;
51+
52+
private StorageType type = memory;
53+
54+
private RedisProperties redis = new RedisProperties();
55+
56+
}
57+
58+
public enum StorageType {
59+
/**
60+
* 内存.
61+
*/
62+
memory,
63+
/**
64+
* redis.
65+
*/
66+
redis
67+
}
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.binarywang.spring.starter.wxjava.open.config.WxOpenAutoConfiguration

0 commit comments

Comments
 (0)