Skip to content

Commit 801a284

Browse files
Copilotbinarywang
andcommitted
修复:根据代码审查反馈优化实现
Co-authored-by: binarywang <[email protected]>
1 parent eaae169 commit 801a284

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

spring-boot-starters/wx-java-pay-multi-spring-boot-starter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public class PayService {
195195
}
196196

197197
// 查询订单
198-
var result = wxPayService.queryOrderV3(null, outTradeNo);
198+
WxPayOrderQueryV3Result result = wxPayService.queryOrderV3(null, outTradeNo);
199199
// 处理查询结果
200200
// ...
201201
}

spring-boot-starters/wx-java-pay-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/service/WxPayMultiServicesImpl.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,15 @@ public WxPayService getWxPayService(String configKey) {
3232
return null;
3333
}
3434

35-
WxPayService wxPayService = services.get(configKey);
36-
if (wxPayService == null) {
37-
synchronized (this) {
38-
wxPayService = services.get(configKey);
39-
if (wxPayService == null) {
40-
WxPaySingleProperties properties = wxPayMultiProperties.getConfigs().get(configKey);
41-
if (properties == null) {
42-
log.warn("未找到配置标识为[{}]的微信支付配置", configKey);
43-
return null;
44-
}
45-
wxPayService = this.buildWxPayService(properties);
46-
services.put(configKey, wxPayService);
47-
}
35+
// 使用 computeIfAbsent 实现线程安全的懒加载,避免使用 synchronized(this) 带来的性能问题
36+
return services.computeIfAbsent(configKey, key -> {
37+
WxPaySingleProperties properties = wxPayMultiProperties.getConfigs().get(key);
38+
if (properties == null) {
39+
log.warn("未找到配置标识为[{}]的微信支付配置", key);
40+
return null;
4841
}
49-
}
50-
return wxPayService;
42+
return this.buildWxPayService(properties);
43+
});
5144
}
5245

5346
@Override

spring-boot-starters/wx-java-pay-multi-spring-boot-starter/src/test/java/com/binarywang/spring/starter/wxjava/pay/example/WxPayMultiExample.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.binarywang.spring.starter.wxjava.pay.example;
22

33
import com.binarywang.spring.starter.wxjava.pay.service.WxPayMultiServices;
4+
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
45
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
6+
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
7+
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
58
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
69
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
710
import com.github.binarywang.wxpay.service.WxPayService;
@@ -137,7 +140,7 @@ public String queryOrderStatus(String appId, String outTradeNo) {
137140
}
138141

139142
// 查询订单
140-
var result = wxPayService.queryOrderV3(null, outTradeNo);
143+
WxPayOrderQueryV3Result result = wxPayService.queryOrderV3(null, outTradeNo);
141144
String tradeState = result.getTradeState();
142145

143146
log.info("查询订单状态成功,appId: {}, outTradeNo: {}, 状态: {}", appId, outTradeNo, tradeState);
@@ -188,7 +191,7 @@ public String refund(String appId, String outTradeNo, Integer refundFee,
188191
request.setAmount(amount);
189192

190193
// 调用微信支付API申请退款
191-
var result = wxPayService.refundV3(request);
194+
WxPayRefundV3Result result = wxPayService.refundV3(request);
192195

193196
log.info("申请退款成功,appId: {}, outTradeNo: {}, outRefundNo: {}",
194197
appId, outTradeNo, request.getOutRefundNo());

0 commit comments

Comments
 (0)