Skip to content

不强制依赖redission,直接项目中排除redisson会cnfe,参考方案 #1437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
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;
Expand All @@ -22,13 +21,9 @@
@Configuration
@RequiredArgsConstructor
public class WxMpStorageAutoConfiguration {
private final WxMpProperties properties;

@Autowired(required = false)
private JedisPool jedisPool;

@Autowired(required = false)
private RedissonClient redissonClient;
private final WxMpProperties properties;
private final ApplicationContext applicationContext;

@Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class)
Expand All @@ -49,13 +44,21 @@ private WxMpDefaultConfigImpl getWxMpInMemoryConfigStorage() {
}

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) {
Expand Down Expand Up @@ -85,8 +88,14 @@ private JedisPool getJedisPool() {
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> T getBean(Class<T> clazz) {
if (this.applicationContext.getBeanNamesForType(clazz, false, false).length > 0) {
return this.applicationContext.getBean(clazz);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
public class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;

/**
* 操作Redis的实现
*/
private ImplType impl = ImplType.jedis;

/**
* 操作Redis的实现如果在spring容器里是否直接使用
*/
private boolean reuseBean = true;

/**
* 主机地址.
*/
Expand Down Expand Up @@ -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
}
}