Skip to content

添加直接获取配置的方法,解决多商户管理场景下的 ThreadLocal 限制#3863

Merged
binarywang merged 3 commits intodevelopfrom
copilot/update-wxpayconfig-holder
Mar 3, 2026
Merged

添加直接获取配置的方法,解决多商户管理场景下的 ThreadLocal 限制#3863
binarywang merged 3 commits intodevelopfrom
copilot/update-wxpayconfig-holder

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

改进 WxPayService 配置获取机制

问题分析

  • 分析当前 WxPayConfigHolder 基于 ThreadLocal 的实现
  • 理解问题:ThreadLocal 在多商户管理场景下的局限性
  • 设计解决方案:添加直接获取配置的方法

实现方案

  • 在 WxPayService 接口中添加 getConfig(String mchId, String appId) 方法
  • 在 WxPayService 接口中添加 getConfig(String mchId) 方法重载
  • 在 BaseWxPayServiceImpl 中实现新方法
  • 保持现有 getConfig() 方法向后兼容

测试

  • 编写单元测试验证新方法
  • 测试多商户场景
  • 验证向后兼容性
  • 测试空参数和不存在配置的处理

文档

  • 更新 MULTI_APPID_USAGE.md 文档
  • 添加新方法的使用示例
  • 添加最佳实践说明

验证

  • 运行构建和代码风格检查 - 通过
  • 确保编译通过 - 通过

根据 Code Review 反馈的优化

  • 性能优化:使用 Stream API 替换 for 循环,提升查找效率并支持提前终止
  • 文档改进:明确说明 getConfig(mchId) 的不确定性和使用场景
  • 使用指导:区分 getConfig 和 switchover 的使用场景,避免混淆

主要改进

  1. 新增方法不依赖 ThreadLocal:可以在任何上下文(异步、线程池等)中安全使用
  2. 向后兼容:保留所有原有方法和行为
  3. 灵活性提升:支持直接获取配置,无需切换上下文
  4. 完整文档:提供详细的使用说明和场景示例
  5. 性能优化:使用 Stream API 实现更高效的配置查找
Original prompt

This section details on the original issue you should resolve

<issue_title>WxPayService#getConfig是否需要改进? WxPayConfigHolder是否有必要替换为生命周期更长的holder?</issue_title>
<issue_description>### 简要描述

在com.github.binarywang.wxpay.service.WxPayService接口中对多商户config的获取在#getConfig中只能通过ThreadLocal间接的获取. 该功能对于需要多商户管理的业务来说较为鸡肋 上下文重新初始化后依然是要重新构建config.

所以, 是否有必要将WxPayConfigHolder改为全局共享的生命周期跟随进程的holder?</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor WxPayConfigHolder for improved lifecycle management 添加直接获取配置的方法,解决多商户管理场景下的 ThreadLocal 限制 Jan 19, 2026
Copilot AI requested a review from binarywang January 19, 2026 04:01
@binarywang binarywang marked this pull request as ready for review January 19, 2026 04:46
Copilot AI review requested due to automatic review settings January 19, 2026 04:46
@augmentcode
Copy link

augmentcode bot commented Jan 19, 2026

🤖 Augment PR Summary

Summary: 本 PR 为微信支付多商户场景补充“直接按参数取配置”的能力,绕开基于 ThreadLocal 的上下文限制。

Changes:

  • WxPayService 新增 getConfig(String mchId, String appId)getConfig(String mchId) 两个重载,用于不依赖线程上下文的配置获取
  • BaseWxPayServiceImpl 实现上述方法:按 mchId_appId 组合 key 精确查找,或按 mchId_ 前缀返回任意匹配项
  • 更新 MULTI_APPID_USAGE.md,补充推荐用法、适用场景与方法对比表
  • MultiAppIdSwitchoverTest 新增测试用例,覆盖精确获取、仅 mchId 获取、无 switchover 场景与空参/不存在配置等情况

Why: 解决异步/线程池下 ThreadLocal 丢失导致无法可靠获取多商户配置的问题,并在保持原有切换接口可用的前提下提供更直接的访问方式。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 为 WxPayService 添加了两个直接获取配置的新方法,解决了多商户管理场景下 ThreadLocal 的限制问题。新增方法不依赖线程上下文,适合在异步、线程池等环境中使用。

Changes:

  • WxPayService 接口中新增 getConfig(String mchId, String appId)getConfig(String mchId) 两个重载方法
  • BaseWxPayServiceImpl 中实现这两个方法,直接从 ConcurrentHashMap 读取配置
  • 添加 8 个测试用例全面覆盖新功能的各种场景(正常、边界、异常情况)
  • 更新 MULTI_APPID_USAGE.md 文档,补充使用场景和最佳实践说明

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java 在接口中添加两个新的 getConfig 重载方法,包含完整的 Javadoc 文档
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java 实现新增的两个 getConfig 方法,包含参数验证、精确匹配和前缀匹配逻辑
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/MultiAppIdSwitchoverTest.java 新增 8 个测试方法,覆盖新功能的各种使用场景和边界条件
weixin-java-pay/MULTI_APPID_USAGE.md 更新文档,新增使用方式说明、场景示例和方法对比表

@binarywang binarywang added this to the 4.8.2 milestone Jan 25, 2026
@binarywang
Copy link
Owner

@copilot apply changes based on the comments in this thread

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot apply changes based on the comments in this thread

已根据 Code Review 反馈完成以下改进(提交 59b728f):

  1. 性能优化:将 getConfig(String mchId) 方法中的 for 循环替换为 Stream API,使用 findFirst() 实现提前终止,提升查找效率

  2. 文档改进

    • 在使用示例中明确说明 getConfig(mchId) 返回结果基于内部存储顺序,不应依赖其稳定性
    • 区分 getConfigswitchover 的使用场景:
      • getConfig 方法:仅用于读取配置信息
      • switchover 方法:用于执行支付操作前的上下文切换
    • 修正场景示例,避免混用两种模式导致困惑
  3. 最佳实践说明:在文档中新增"使用场景"部分,帮助开发者正确选择使用方式

@binarywang
Copy link
Owner

@copilot apply changes based on the comments in this thread

Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot apply changes based on the comments in this thread

所有的代码审查反馈已在提交 59b728f 中完成应用:

已完成的改进:

  1. 性能优化:使用 Stream API 替换 for 循环(BaseWxPayServiceImpl.java 185-195行)
  2. 文档改进:明确说明 getConfig(mchId) 的不确定性和使用场景
  3. 示例修正:修正 processRefund 方法示例,避免混用模式

所有三个审查线程均已标记为已解决。如果需要其他修改,请告知具体需求。

@binarywang binarywang merged commit d661d0c into develop Mar 3, 2026
2 checks passed
@binarywang binarywang deleted the copilot/update-wxpayconfig-holder branch March 3, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WxPayService#getConfig是否需要改进? WxPayConfigHolder是否有必要替换为生命周期更长的holder?

3 participants