From e761174270be4a5bd3fa553989e18a6b97f87032 Mon Sep 17 00:00:00 2001 From: miemieYaho Date: Thu, 12 Mar 2020 17:10:58 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#1437=20=E5=85=AC=E4=BC=97=E5=8F=B7spri?= =?UTF-8?q?ng=20boot=20starter=E6=A8=A1=E5=9D=97=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=BC=BA=E5=88=B6=E4=BE=9D=E8=B5=96redission?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不强制依赖redission,直接项目中排除redisson会cnfe,参考方案 --- .../wx-java-mp-spring-boot-starter/pom.xml | 1 + .../config/WxMpStorageAutoConfiguration.java | 43 +++++++++++-------- .../wxjava/mp/properties/RedisProperties.java | 21 +++++++++ 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml b/spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml index 117324d9a..9f741a788 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml @@ -28,6 +28,7 @@ org.redisson redisson compile + true diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java index 4ddff5a72..f25caf90b 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java @@ -6,9 +6,8 @@ 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 org.redisson.api.RedissonClient; -import org.springframework.beans.factory.annotation.Autowired; 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 redis.clients.jedis.JedisPool; @@ -22,13 +21,9 @@ import redis.clients.jedis.JedisPoolConfig; @Configuration @RequiredArgsConstructor public class WxMpStorageAutoConfiguration { + private final WxMpProperties properties; - - @Autowired(required = false) - private JedisPool jedisPool; - - @Autowired(required = false) - private RedissonClient redissonClient; + private final ApplicationContext applicationContext; @Bean @ConditionalOnMissingBean(WxMpConfigStorage.class) @@ -49,13 +44,21 @@ public class WxMpStorageAutoConfiguration { } private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() { - JedisPool poolToUse = jedisPool; - if (poolToUse == null) { - poolToUse = getJedisPool(); + 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; } - WxMpRedisConfigImpl config = new WxMpRedisConfigImpl(poolToUse); - setWxMpInfo(config); - return config; + throw new UnsupportedOperationException(); } private void setWxMpInfo(WxMpDefaultConfigImpl config) { @@ -85,8 +88,14 @@ public class WxMpStorageAutoConfiguration { config.setTestOnBorrow(true); config.setTestWhileIdle(true); - JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), - redis.getTimeout(), redis.getPassword(), redis.getDatabase()); - return pool; + return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), + redis.getDatabase()); + } + + private T getBean(Class clazz) { + if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) { + return this.applicationContext.getBean(clazz); + } + return null; } } diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java index d95e8df98..840cf6e35 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java @@ -13,6 +13,16 @@ import java.io.Serializable; public class RedisProperties implements Serializable { private static final long serialVersionUID = -5924815351660074401L; + /** + * 操作Redis的实现 + */ + private ImplType impl = ImplType.jedis; + + /** + * 操作Redis的实现如果在spring容器里是否直接使用 + */ + private boolean reuseBean = true; + /** * 主机地址. */ @@ -42,4 +52,15 @@ public class RedisProperties implements Serializable { private Integer maxIdle; private Integer maxWaitMillis; private Integer minIdle; + + public enum ImplType { + /** + * jedis. + */ + jedis, + /** + * redisson. + */ +// redisson + } }