修复ProxyUtil可能的空指针问题(issue#IBF20Z@Gitee)

This commit is contained in:
Looly 2025-01-02 13:33:02 +08:00
parent 2500d5307c
commit 8c81e43a07
4 changed files with 63 additions and 2 deletions

View File

@ -2,11 +2,12 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.36(2025-01-01)
# 5.8.36(2025-01-02)
### 🐣新特性
* 【crypto 】 增加BCUtil.decodeECPrivateKey方法issue#3829@Github
### 🐞Bug修复
* 【aop 】 修复ProxyUtil可能的空指针问题issue#IBF20Z@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.35(2024-12-25)

View File

@ -12,6 +12,11 @@ import cn.hutool.aop.interceptor.JdkInterceptor;
public class JdkProxyFactory extends ProxyFactory {
private static final long serialVersionUID = 1L;
/**
* 获取单例
*/
public static JdkProxyFactory INSTANCE = new JdkProxyFactory();
@Override
public <T> T proxy(T target, Aspect aspect) {
return ProxyUtil.newProxyInstance(//

View File

@ -59,7 +59,13 @@ public abstract class ProxyFactory implements Serializable {
* @return 代理对象
*/
public static <T> T createProxy(T target, Aspect aspect) {
return create().proxy(target, aspect);
ProxyFactory factory = create();
if(null == factory){
// issue#IBF20Z
// 可能的空指针问题
factory = JdkProxyFactory.INSTANCE;
}
return factory.proxy(target, aspect);
}
/**

View File

@ -0,0 +1,49 @@
package cn.hutool.aop.test;
import cn.hutool.aop.proxy.ProxyFactory;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class IssueIBF20ZTest {
@Test
public void testLoadFirstAvailableConcurrent() throws InterruptedException {
// 创建一个固定大小的线程池
int threadCount = 1000;
ExecutorService executorService = ThreadUtil.newExecutor(threadCount);
// 创建一个 CountDownLatch用于等待所有任务完成
CountDownLatch latch = new CountDownLatch(threadCount);
// 计数器用于统计成功加载服务提供者的次数
AtomicInteger successCount = new AtomicInteger(0);
// 提交多个任务到线程池
for (int i = 0; i < threadCount; i++) {
executorService.submit(() -> {
ProxyFactory factory = ProxyFactory.create();
if (factory != null) {
Console.log(factory.getClass());
successCount.incrementAndGet();
}
latch.countDown(); // 每个任务完成时计数减一
});
}
// 等待所有任务完成
latch.await();
// 关闭线程池并等待所有任务完成
executorService.shutdown();
// 验证所有线程都成功加载了服务提供者
assertEquals(threadCount, successCount.get());
}
}