Repository navigation
fix(wx-java-mp): 修复 Spring Boot Starter 的 Redisson 客户端回退 #4147
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
Closed
miemieYaho
wants to merge
2
commits into
binarywang:develop
from
miemieYaho:codex/wxmp-redisson-fallback
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...spring/starter/wxjava/mp/config/storage/WxMpInRedissonConfigStorageConfigurationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.binarywang.spring.starter.wxjava.mp.config.storage; | ||
|
|
||
| import com.binarywang.spring.starter.wxjava.mp.config.WxMpAutoConfiguration; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.springframework.beans.factory.support.AbstractBeanDefinition; | ||
| import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
| import org.springframework.core.env.MapPropertySource; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.atLeastOnce; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertSame; | ||
|
|
||
| public class WxMpInRedissonConfigStorageConfigurationTest { | ||
| private static final String CLIENT_BEAN_NAME = "wxMpRedissonClient"; | ||
|
|
||
| @Test | ||
| public void reusesExistingClientWithoutDedicatedHost() { | ||
| RedissonClient sharedClient = mock(RedissonClient.class); | ||
| try (AnnotationConfigApplicationContext context = context(null, sharedClient)) { | ||
| context.refresh(); | ||
|
|
||
| assertFalse(context.containsBean(CLIENT_BEAN_NAME)); | ||
| verify(sharedClient, atLeastOnce()).getLock(anyString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void usesDedicatedClientWithExplicitHost() { | ||
| RedissonClient sharedClient = mock(RedissonClient.class); | ||
| RedissonClient dedicatedClient = mock(RedissonClient.class); | ||
| try (AnnotationConfigApplicationContext context = context("redis.example.test", sharedClient)) { | ||
| // Verify that Spring registers the dedicated bean without opening a Redis connection. | ||
| context.addBeanFactoryPostProcessor(beanFactory -> { | ||
| AbstractBeanDefinition definition = (AbstractBeanDefinition) beanFactory.getBeanDefinition(CLIENT_BEAN_NAME); | ||
| definition.setInstanceSupplier(() -> dedicatedClient); | ||
| }); | ||
| context.refresh(); | ||
|
|
||
| assertSame(context.getBean(CLIENT_BEAN_NAME), dedicatedClient); | ||
| verify(dedicatedClient, atLeastOnce()).getLock(anyString()); | ||
| } | ||
| } | ||
|
|
||
| private AnnotationConfigApplicationContext context(String host, RedissonClient sharedClient) { | ||
| AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); | ||
| Map<String, Object> properties = new HashMap<>(); | ||
| properties.put("wx.mp.app-id", "test-app"); | ||
| properties.put("wx.mp.config-storage.type", "redisson"); | ||
| if (host != null) { | ||
| properties.put("wx.mp.config-storage.redis.host", host); | ||
| } | ||
| context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("test", properties)); | ||
| context.registerBean("sharedRedissonClient", RedissonClient.class, () -> sharedClient); | ||
| context.register(WxMpAutoConfiguration.class); | ||
| return context; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这次修复的核心依赖于 Spring 解析
@Conditional后是否注册wxMpRedissonClient,但提交中没有任何测试覆盖,现有的 compile 验证不会执行这里的条件或 bean 查找。请至少用应用上下文分别覆盖“省略redis.host且应用提供RedissonClient”和“显式配置 host 后创建专用 bean”的启动场景,否则原始初始化失败很容易在后续改动中回归。AGENTS.md reference: AGENTS.md:L64-L64
Useful? React with 👍 / 👎.