🎨 代码优化,防止出现空指针异常报错

Co-authored-by: sunhui <10151317@qq.com>
This commit is contained in:
playersun 2023-10-11 00:18:57 +08:00 committed by Binary Wang
parent 4e67c56450
commit 98881f2e76
2 changed files with 22 additions and 4 deletions

View File

@ -377,7 +377,12 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
@Override
@JsonDeserialize
public void setMultiConfigs(Map<String, WxMaConfig> configs, String defaultMiniappId) {
this.configMap = Maps.newHashMap(configs);
// 防止覆盖配置
if(this.configMap != null) {
this.configMap.putAll(configs);
} else {
this.configMap = Maps.newHashMap(configs);
}
WxMaConfigHolder.set(defaultMiniappId);
this.initHttp();
}
@ -385,7 +390,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
@Override
public void addConfig(String miniappId, WxMaConfig configStorages) {
synchronized (this) {
if (this.configMap == null) {
/*
* 因为commit f74b00cf 默认初始化了configMap导致使用此方法无法进入if从而触发initHttp()
* 就会出现HttpClient报NullPointException
*/
if (this.configMap == null || this.configMap.isEmpty()) {
this.setWxMaConfig(configStorages);
} else {
WxMaConfigHolder.set(miniappId);

View File

@ -542,7 +542,12 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override
public void setMultiConfigStorages(Map<String, WxMpConfigStorage> configStorages, String defaultMpId) {
this.configStorageMap = Maps.newHashMap(configStorages);
// 防止覆盖配置
if(this.configStorageMap != null) {
this.configStorageMap.putAll(configStorages);
} else {
this.configStorageMap = Maps.newHashMap(configStorages);
}
WxMpConfigStorageHolder.set(defaultMpId);
this.initHttp();
}
@ -550,7 +555,11 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override
public void addConfigStorage(String mpId, WxMpConfigStorage configStorages) {
synchronized (this) {
if (this.configStorageMap == null) {
/*
* 因为commit 2aa27cf12d 默认初始化了configStorageMap导致使用此方法无法进入if从而触发initHttp()
* 就会出现HttpClient报NullPointException
*/
if (this.configStorageMap == null || this.configStorageMap.isEmpty()) {
this.setWxMpConfigStorage(configStorages);
} else {
WxMpConfigStorageHolder.set(mpId);