🔨 规范代码

This commit is contained in:
Binary Wang 2019-08-10 17:43:14 +08:00
parent 44b3680a3f
commit 3a1fae639a
5 changed files with 143 additions and 121 deletions

View File

@ -5,6 +5,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
/**
* .
*
* @author someone
*/
@Configuration @Configuration
@EnableConfigurationProperties(WxMpProperties.class) @EnableConfigurationProperties(WxMpProperties.class)
@Import({WxMpStorageAutoConfiguration.class, WxMpServiceAutoConfiguration.class}) @Import({WxMpStorageAutoConfiguration.class, WxMpServiceAutoConfiguration.class})

View File

@ -12,42 +12,44 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
* 微信公众号相关服务自动注册 * 微信公众号相关服务自动注册.
*
* @author someone
*/ */
@Configuration @Configuration
public class WxMpServiceAutoConfiguration { public class WxMpServiceAutoConfiguration {
@Autowired @Autowired
private ApplicationContext ctx; private ApplicationContext ctx;
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WxMpService wxMpService(WxMpConfigStorage configStorage) { public WxMpService wxMpService(WxMpConfigStorage configStorage) {
WxMpService wxMpService = new WxMpServiceImpl(); WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(configStorage); wxMpService.setWxMpConfigStorage(configStorage);
registerWxMpSubService(wxMpService); registerWxMpSubService(wxMpService);
return wxMpService; return wxMpService;
} }
@ConditionalOnBean(WxMpService.class) @ConditionalOnBean(WxMpService.class)
public Object registerWxMpSubService(WxMpService wxMpService) { public Object registerWxMpSubService(WxMpService wxMpService) {
ConfigurableListableBeanFactory factory = (ConfigurableListableBeanFactory) ctx.getAutowireCapableBeanFactory(); ConfigurableListableBeanFactory factory = (ConfigurableListableBeanFactory) ctx.getAutowireCapableBeanFactory();
factory.registerSingleton("wxMpKefuService", wxMpService.getKefuService()); factory.registerSingleton("wxMpKefuService", wxMpService.getKefuService());
factory.registerSingleton("wxMpMaterialService", wxMpService.getMaterialService()); factory.registerSingleton("wxMpMaterialService", wxMpService.getMaterialService());
factory.registerSingleton("wxMpMenuService", wxMpService.getMenuService()); factory.registerSingleton("wxMpMenuService", wxMpService.getMenuService());
factory.registerSingleton("wxMpUserService", wxMpService.getUserService()); factory.registerSingleton("wxMpUserService", wxMpService.getUserService());
factory.registerSingleton("wxMpUserTagService", wxMpService.getUserTagService()); factory.registerSingleton("wxMpUserTagService", wxMpService.getUserTagService());
factory.registerSingleton("wxMpQrcodeService", wxMpService.getQrcodeService()); factory.registerSingleton("wxMpQrcodeService", wxMpService.getQrcodeService());
factory.registerSingleton("wxMpCardService", wxMpService.getCardService()); factory.registerSingleton("wxMpCardService", wxMpService.getCardService());
factory.registerSingleton("wxMpDataCubeService", wxMpService.getDataCubeService()); factory.registerSingleton("wxMpDataCubeService", wxMpService.getDataCubeService());
factory.registerSingleton("wxMpUserBlacklistService", wxMpService.getBlackListService()); factory.registerSingleton("wxMpUserBlacklistService", wxMpService.getBlackListService());
factory.registerSingleton("wxMpStoreService", wxMpService.getStoreService()); factory.registerSingleton("wxMpStoreService", wxMpService.getStoreService());
factory.registerSingleton("wxMpTemplateMsgService", wxMpService.getTemplateMsgService()); factory.registerSingleton("wxMpTemplateMsgService", wxMpService.getTemplateMsgService());
factory.registerSingleton("wxMpSubscribeMsgService", wxMpService.getSubscribeMsgService()); factory.registerSingleton("wxMpSubscribeMsgService", wxMpService.getSubscribeMsgService());
factory.registerSingleton("wxMpDeviceService", wxMpService.getDeviceService()); factory.registerSingleton("wxMpDeviceService", wxMpService.getDeviceService());
factory.registerSingleton("wxMpShakeService", wxMpService.getShakeService()); factory.registerSingleton("wxMpShakeService", wxMpService.getShakeService());
factory.registerSingleton("wxMpMemberCardService", wxMpService.getMemberCardService()); factory.registerSingleton("wxMpMemberCardService", wxMpService.getMemberCardService());
factory.registerSingleton("wxMpMassMessageService", wxMpService.getMassMessageService()); factory.registerSingleton("wxMpMassMessageService", wxMpService.getMassMessageService());
return Boolean.TRUE; return Boolean.TRUE;
} }
} }

View File

@ -2,6 +2,7 @@ package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; 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.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
@ -13,74 +14,75 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
/** /**
* 微信公众号存储策略自动配置 * 微信公众号存储策略自动配置.
*
* @author someone
*/ */
@Configuration @Configuration
@RequiredArgsConstructor
public class WxMpStorageAutoConfiguration { public class WxMpStorageAutoConfiguration {
private final WxMpProperties properties;
@Autowired @Autowired(required = false)
private WxMpProperties properties; private JedisPool jedisPool;
@Autowired(required = false) @Bean
private JedisPool jedisPool; @ConditionalOnMissingBean(WxMpConfigStorage.class)
public WxMpConfigStorage wxMpInMemoryConfigStorage() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
WxMpProperties.StorageType type = storage.getType();
@Bean if (type == WxMpProperties.StorageType.redis) {
@ConditionalOnMissingBean(WxMpConfigStorage.class) return getWxMpInRedisConfigStorage();
public WxMpConfigStorage wxMpInMemoryConfigStorage() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
WxMpProperties.StorageType type = storage.getType();
if (type == WxMpProperties.StorageType.redis) {
return getWxMpInRedisConfigStorage();
}
return getWxMpInMemoryConfigStorage();
} }
return getWxMpInMemoryConfigStorage();
}
private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() { private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() {
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
setWxMpInfo(config); setWxMpInfo(config);
return config; return config;
}
private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() {
JedisPool poolToUse = jedisPool;
if (poolToUse == null) {
poolToUse = getJedisPool();
} }
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(poolToUse);
setWxMpInfo(config);
return config;
}
private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() { private void setWxMpInfo(WxMpDefaultConfigImpl config) {
JedisPool poolToUse = jedisPool; config.setAppId(properties.getAppId());
if (poolToUse == null) { config.setSecret(properties.getSecret());
poolToUse = getJedisPool(); config.setToken(properties.getToken());
} config.setAesKey(properties.getAesKey());
WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(poolToUse); }
setWxMpInfo(config);
return config; private JedisPool getJedisPool() {
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
RedisProperties redis = storage.getRedis();
JedisPoolConfig config = new JedisPoolConfig();
if (redis.getMaxActive() != null) {
config.setMaxTotal(redis.getMaxActive());
} }
if (redis.getMaxIdle() != null) {
private void setWxMpInfo(WxMpDefaultConfigImpl config) { config.setMaxIdle(redis.getMaxIdle());
config.setAppId(properties.getAppId());
config.setSecret(properties.getSecret());
config.setToken(properties.getToken());
config.setAesKey(properties.getAesKey());
} }
if (redis.getMaxWaitMillis() != null) {
private JedisPool getJedisPool() { config.setMaxWaitMillis(redis.getMaxWaitMillis());
WxMpProperties.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;
} }
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;
}
} }

View File

@ -5,38 +5,41 @@ import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
/** /**
* Redis配置 * Redis配置.
*
* @author someone
*/ */
@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 String host = "127.0.0.1";
/** /**
* 端口号 * 端口号.
*/ */
private int port = 6379; private int port = 6379;
/** /**
* 密码 * 密码.
*/ */
private String password; private String password;
/** /**
* 超时 * 超时.
*/ */
private int timeout = 2000; private int timeout = 2000;
/** /**
* 数据库 * 数据库.
*/ */
private int database = 0; private int database = 0;
private Integer maxActive; private Integer maxActive;
private Integer maxIdle; private Integer maxIdle;
private Integer maxWaitMillis; private Integer maxWaitMillis;
private Integer minIdle; private Integer minIdle;
} }

View File

@ -10,7 +10,9 @@ import static com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties.
/** /**
* 微信接入相关配置属性 * 微信接入相关配置属性.
*
* @author someone
*/ */
@Data @Data
@ConfigurationProperties(PREFIX) @ConfigurationProperties(PREFIX)
@ -18,33 +20,34 @@ public class WxMpProperties {
public static final String PREFIX = "wx.mp"; public static final String PREFIX = "wx.mp";
/** /**
* 设置微信公众号的appid * 设置微信公众号的appid.
*/ */
private String appId; private String appId;
/** /**
* 设置微信公众号的app secret * 设置微信公众号的app secret.
*/ */
private String secret; private String secret;
/** /**
* 设置微信公众号的token * 设置微信公众号的token.
*/ */
private String token; private String token;
/** /**
* 设置微信公众号的EncodingAESKey * 设置微信公众号的EncodingAESKey.
*/ */
private String aesKey; private String aesKey;
/** /**
* 存储策略, memory, redis * 存储策略, memory, redis.
*/ */
private ConfigStorage configStorage = new ConfigStorage(); private ConfigStorage configStorage = new ConfigStorage();
@Data @Data
public static class ConfigStorage implements Serializable { public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;
private StorageType type = memory; private StorageType type = memory;
@ -53,6 +56,13 @@ public class WxMpProperties {
} }
public enum StorageType { public enum StorageType {
memory, redis /**
* 内存.
*/
memory,
/**
* redis.
*/
redis
} }
} }