mirror of
https://gitee.com/zhijiantianya/ruoyi-vue-pro.git
synced 2026-10-06 07:22:59 +08:00
feat(system):TenantUtils 增加 isTenantEnable 是否开启租户的开关
This commit is contained in:
+18
@@ -1,9 +1,12 @@
|
||||
package cn.iocoder.yudao.framework.tenant.core.util;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import com.google.common.base.Suppliers;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID;
|
||||
|
||||
@@ -14,6 +17,21 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_
|
||||
*/
|
||||
public class TenantUtils {
|
||||
|
||||
/**
|
||||
* 多租户开关,首次使用时读取并缓存
|
||||
*/
|
||||
private static final Supplier<Boolean> TENANT_ENABLE = Suppliers.memoize(
|
||||
() -> !Boolean.FALSE.equals(SpringUtil.getProperty("yudao.tenant.enable", Boolean.class, true)));
|
||||
|
||||
/**
|
||||
* 判断是否开启多租户
|
||||
*
|
||||
* @return 是否开启多租户
|
||||
*/
|
||||
public static boolean isTenantEnable() {
|
||||
return TENANT_ENABLE.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定租户,执行对应的逻辑
|
||||
*
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.yudao.framework.tenant.core.util;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
class TenantUtilsTest {
|
||||
|
||||
@Test
|
||||
void shouldReadTenantEnableOnlyOnce() {
|
||||
// 准备多租户开关
|
||||
try (MockedStatic<SpringUtil> springUtil = mockStatic(SpringUtil.class)) {
|
||||
springUtil.when(() -> SpringUtil.getProperty("yudao.tenant.enable", Boolean.class, true))
|
||||
.thenReturn(true);
|
||||
|
||||
// 多次访问只读取一次配置
|
||||
assertTrue(TenantUtils.isTenantEnable());
|
||||
assertTrue(TenantUtils.isTenantEnable());
|
||||
springUtil.verify(() -> SpringUtil.getProperty("yudao.tenant.enable", Boolean.class, true), times(1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user