| @@ -9,45 +9,44 @@ | |||||
| </parent> | </parent> | ||||
| <modelVersion>4.0.0</modelVersion> | <modelVersion>4.0.0</modelVersion> | ||||
| <artifactId>wx-java-open-spring-boot-starter</artifactId> | |||||
| <name>WxJava - Spring Boot Starter for WxOpen</name> | |||||
| <description>微信开放平台开发的 Spring Boot Starter</description> | |||||
| <artifactId>wx-java-open-spring-boot-starter</artifactId> | |||||
| <name>WxJava - Spring Boot Starter for WxOpen</name> | |||||
| <description>微信开放平台开发的 Spring Boot Starter</description> | |||||
| <dependencies> | |||||
| <dependencies> | |||||
| <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> | |||||
| </dependencies> | |||||
| <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> | |||||
| </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> | |||||
| <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> | </project> | ||||
| @@ -17,23 +17,23 @@ import org.springframework.context.annotation.Configuration; | |||||
| @Configuration | @Configuration | ||||
| public class WxOpenServiceAutoConfiguration { | public class WxOpenServiceAutoConfiguration { | ||||
| @Bean | |||||
| @ConditionalOnMissingBean | |||||
| public WxOpenService wxOpenService(WxOpenConfigStorage configStorage) { | |||||
| WxOpenService wxOpenService = new WxOpenServiceImpl(); | |||||
| wxOpenService.setWxOpenConfigStorage(configStorage); | |||||
| return wxOpenService; | |||||
| } | |||||
| @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 WxOpenMessageRouter wxOpenMessageRouter(WxOpenService wxOpenService) { | |||||
| return new WxOpenMessageRouter(wxOpenService); | |||||
| } | |||||
| @Bean | |||||
| public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) { | |||||
| return wxOpenService.getWxOpenComponentService(); | |||||
| } | |||||
| @Bean | |||||
| public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) { | |||||
| return wxOpenService.getWxOpenComponentService(); | |||||
| } | |||||
| } | } | ||||
| @@ -23,71 +23,71 @@ import redis.clients.jedis.JedisPoolConfig; | |||||
| @Configuration | @Configuration | ||||
| @RequiredArgsConstructor | @RequiredArgsConstructor | ||||
| public class WxOpenStorageAutoConfiguration { | public class WxOpenStorageAutoConfiguration { | ||||
| private final WxOpenProperties properties; | |||||
| private final WxOpenProperties properties; | |||||
| @Autowired(required = false) | |||||
| private JedisPool jedisPool; | |||||
| @Autowired(required = false) | |||||
| private JedisPool jedisPool; | |||||
| @Value("${wx.open.config-storage.redis.host:}") | |||||
| private String redisHost; | |||||
| @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(); | |||||
| @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(); | |||||
| 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 WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() { | |||||
| WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage(); | |||||
| 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 WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() { | |||||
| JedisPool poolToUse = jedisPool; | |||||
| if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) { | |||||
| poolToUse = getJedisPool(); | |||||
| } | } | ||||
| WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse); | |||||
| setWxOpenInfo(config); | |||||
| return config; | |||||
| } | |||||
| private JedisPool getJedisPool() { | |||||
| WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); | |||||
| RedisProperties redis = storage.getRedis(); | |||||
| private void setWxOpenInfo(WxOpenConfigStorage config) { | |||||
| config.setComponentAppId(properties.getAppId()); | |||||
| config.setComponentAppSecret(properties.getSecret()); | |||||
| config.setComponentToken(properties.getToken()); | |||||
| config.setComponentAesKey(properties.getAesKey()); | |||||
| } | |||||
| 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); | |||||
| private JedisPool getJedisPool() { | |||||
| WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); | |||||
| RedisProperties redis = storage.getRedis(); | |||||
| JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), | |||||
| redis.getTimeout(), redis.getPassword(), redis.getDatabase()); | |||||
| return pool; | |||||
| 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; | |||||
| } | |||||
| } | } | ||||
| @@ -11,35 +11,35 @@ import java.io.Serializable; | |||||
| */ | */ | ||||
| @Data | @Data | ||||
| public class RedisProperties implements Serializable { | 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; | |||||
| 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; | |||||
| } | } | ||||
| @@ -17,52 +17,52 @@ import static com.binarywang.spring.starter.wxjava.open.properties.WxOpenPropert | |||||
| @Data | @Data | ||||
| @ConfigurationProperties(PREFIX) | @ConfigurationProperties(PREFIX) | ||||
| public class WxOpenProperties { | public class WxOpenProperties { | ||||
| public static final String PREFIX = "wx.open"; | |||||
| public static final String PREFIX = "wx.open"; | |||||
| /** | |||||
| * 设置微信开放平台的appid. | |||||
| */ | |||||
| private String appId; | |||||
| /** | |||||
| * 设置微信开放平台的appid. | |||||
| */ | |||||
| private String appId; | |||||
| /** | |||||
| * 设置微信开放平台的app secret. | |||||
| */ | |||||
| private String secret; | |||||
| /** | |||||
| * 设置微信开放平台的app secret. | |||||
| */ | |||||
| private String secret; | |||||
| /** | |||||
| * 设置微信开放平台的token. | |||||
| */ | |||||
| private String token; | |||||
| /** | |||||
| * 设置微信开放平台的token. | |||||
| */ | |||||
| private String token; | |||||
| /** | |||||
| * 设置微信开放平台的EncodingAESKey. | |||||
| */ | |||||
| private String aesKey; | |||||
| /** | |||||
| * 设置微信开放平台的EncodingAESKey. | |||||
| */ | |||||
| private String aesKey; | |||||
| /** | |||||
| * 存储策略, memory, redis. | |||||
| */ | |||||
| private ConfigStorage configStorage = new ConfigStorage(); | |||||
| /** | |||||
| * 存储策略, memory, redis. | |||||
| */ | |||||
| private ConfigStorage configStorage = new ConfigStorage(); | |||||
| @Data | |||||
| public static class ConfigStorage implements Serializable { | |||||
| private static final long serialVersionUID = 4815731027000065434L; | |||||
| @Data | |||||
| public static class ConfigStorage implements Serializable { | |||||
| private static final long serialVersionUID = 4815731027000065434L; | |||||
| private StorageType type = memory; | |||||
| private StorageType type = memory; | |||||
| private RedisProperties redis = new RedisProperties(); | |||||
| private RedisProperties redis = new RedisProperties(); | |||||
| } | |||||
| } | |||||
| public enum StorageType { | |||||
| /** | |||||
| * 内存. | |||||
| */ | |||||
| memory, | |||||
| /** | |||||
| * redis. | |||||
| */ | |||||
| redis | |||||
| } | |||||
| public enum StorageType { | |||||
| /** | |||||
| * 内存. | |||||
| */ | |||||
| memory, | |||||
| /** | |||||
| * redis. | |||||
| */ | |||||
| redis | |||||
| } | |||||
| } | } | ||||