🎨 #1437 公众号spring boot starter模块优化,不强制依赖redission

不强制依赖redission,直接项目中排除redisson会cnfe,参考方案
This commit is contained in:
miemieYaho 2020-03-12 17:10:58 +08:00 committed by GitHub
parent 341f176dfa
commit e761174270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 17 deletions

View File

@ -28,6 +28,7 @@
<groupId>org.redisson</groupId> <groupId>org.redisson</groupId>
<artifactId>redisson</artifactId> <artifactId>redisson</artifactId>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -6,9 +6,8 @@ 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;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
@ -22,13 +21,9 @@ import redis.clients.jedis.JedisPoolConfig;
@Configuration @Configuration
@RequiredArgsConstructor @RequiredArgsConstructor
public class WxMpStorageAutoConfiguration { public class WxMpStorageAutoConfiguration {
private final WxMpProperties properties; private final WxMpProperties properties;
private final ApplicationContext applicationContext;
@Autowired(required = false)
private JedisPool jedisPool;
@Autowired(required = false)
private RedissonClient redissonClient;
@Bean @Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class) @ConditionalOnMissingBean(WxMpConfigStorage.class)
@ -49,13 +44,21 @@ public class WxMpStorageAutoConfiguration {
} }
private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() { private WxMpRedisConfigImpl getWxMpInRedisConfigStorage() {
JedisPool poolToUse = jedisPool; RedisProperties.ImplType implType = properties.getConfigStorage().getRedis().getImpl();
if (poolToUse == null) { boolean reuseBean = properties.getConfigStorage().getRedis().isReuseBean();
poolToUse = getJedisPool(); 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); throw new UnsupportedOperationException();
setWxMpInfo(config);
return config;
} }
private void setWxMpInfo(WxMpDefaultConfigImpl config) { private void setWxMpInfo(WxMpDefaultConfigImpl config) {
@ -85,8 +88,14 @@ public class WxMpStorageAutoConfiguration {
config.setTestOnBorrow(true); config.setTestOnBorrow(true);
config.setTestWhileIdle(true); config.setTestWhileIdle(true);
JedisPool pool = new JedisPool(config, redis.getHost(), redis.getPort(), return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
redis.getTimeout(), redis.getPassword(), redis.getDatabase()); redis.getDatabase());
return pool; }
private <T> T getBean(Class<T> clazz) {
if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
return this.applicationContext.getBean(clazz);
}
return null;
} }
} }

View File

@ -13,6 +13,16 @@ import java.io.Serializable;
public class RedisProperties implements Serializable { public class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L; 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 maxIdle;
private Integer maxWaitMillis; private Integer maxWaitMillis;
private Integer minIdle; private Integer minIdle;
public enum ImplType {
/**
* jedis.
*/
jedis,
/**
* redisson.
*/
// redisson
}
} }