From ba2bd3f6a5648cb738b816f959bf6e53269ea293 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Mon, 15 Jan 2024 09:23:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202.6.0=201.=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E5=8F=82=E6=95=B0=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/context/SpringContext.java | 17 +++++++++++++---- .../common/core/util/EnvironmentUtils.java | 19 ++++++++++++++----- .../aizuda/easy/retry/common/log/Local.java | 9 ++++----- .../aizuda/easy/retry/common/log/Remote.java | 14 +++++++------- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/context/SpringContext.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/context/SpringContext.java index 1e486666..20ecacf9 100644 --- a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/context/SpringContext.java +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/context/SpringContext.java @@ -32,8 +32,17 @@ public class SpringContext implements ApplicationContextAware { public static synchronized T getBean(String name) { try { return (T) CONTEXT.getBean(name); - } catch (BeansException var2) { - log.error(" BeanName:{} not exist,Exception => {}", name, var2.getMessage()); + } catch (BeansException | NullPointerException exception) { + log.error(" BeanName:{} not exist,Exception => {}", name, exception.getMessage()); + return null; + } + } + + public static synchronized T getBean(Class requiredType) { + try { + return CONTEXT.getBean(requiredType); + } catch (BeansException | NullPointerException exception) { + log.error(" BeanName:{} not exist,Exception => {}", requiredType.getName(), exception.getMessage()); return null; } } @@ -41,8 +50,8 @@ public class SpringContext implements ApplicationContextAware { public static synchronized T getBean(String name, Class requiredType) { try { return CONTEXT.getBean(name, requiredType); - } catch (BeansException | NullPointerException var3) { - log.error(" BeanName:{} not exist,Exception => {}", name, var3.getMessage()); + } catch (BeansException | NullPointerException exception) { + log.error(" BeanName:{} not exist,Exception => {}", name, exception.getMessage()); return null; } } diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/EnvironmentUtils.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/EnvironmentUtils.java index d6715a49..5d37e544 100644 --- a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/EnvironmentUtils.java +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/util/EnvironmentUtils.java @@ -3,6 +3,8 @@ package com.aizuda.easy.retry.common.core.util; import com.aizuda.easy.retry.common.core.context.SpringContext; import org.springframework.core.env.Environment; +import java.util.Objects; + /** * 获取环境信息 * @@ -10,8 +12,6 @@ import org.springframework.core.env.Environment; * @date : 2021-12-01 14:27 */ public class EnvironmentUtils { - - private static final Environment environment = SpringContext.CONTEXT.getBean(Environment.class); public static final String DEFAULT_ENV = "default "; /** @@ -21,8 +21,12 @@ public class EnvironmentUtils { */ public static Boolean getLogStatus() { - Environment environment = SpringContext.CONTEXT.getBean(Environment.class); - return environment.getProperty("easy.retry.log.status", Boolean.class, Boolean.TRUE); + Environment environment = SpringContext.getBean(Environment.class); + if (Objects.nonNull(environment)) { + return environment.getProperty("easy.retry.log.status", Boolean.class, Boolean.TRUE); + } + + return Boolean.TRUE; } /** @@ -32,6 +36,11 @@ public class EnvironmentUtils { */ public static String getActiveProfile() { + Environment environment = SpringContext.getBean(Environment.class); + if (Objects.isNull(environment)) { + return DEFAULT_ENV; + } + String[] activeProfiles = environment.getActiveProfiles(); if (activeProfiles.length == 0) { return DEFAULT_ENV; @@ -39,7 +48,7 @@ public class EnvironmentUtils { StringBuilder envs = new StringBuilder(); for (String activeProfile : activeProfiles) { - envs.append(activeProfile + " "); + envs.append(activeProfile).append(" "); } return envs.toString(); } diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Local.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Local.java index e46fb56f..490b9033 100644 --- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Local.java +++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Local.java @@ -1,6 +1,5 @@ package com.aizuda.easy.retry.common.log; -import cn.hutool.core.util.StrUtil; import com.aizuda.easy.retry.common.core.util.EnvironmentUtils; import com.aizuda.easy.retry.common.log.lang.LogCaller; import com.aizuda.easy.retry.common.log.level.Level; @@ -63,7 +62,7 @@ public final class Local { return; } - debug(LogFactory.get(LogCaller.getCallerCaller()), format, false, arguments); + debug(LogFactory.get(LogCaller.getCallerCaller()), format, arguments); } /** @@ -127,7 +126,7 @@ public final class Local { return; } - warn(LogFactory.get(LogCaller.getCallerCaller()), format, false, arguments); + warn(LogFactory.get(LogCaller.getCallerCaller()), format, arguments); } /** @@ -143,7 +142,7 @@ public final class Local { return; } - warn(LogFactory.get(LogCaller.getCallerCaller()), e, StrUtil.format(format, arguments), false); + warn(LogFactory.get(LogCaller.getCallerCaller()), e, format, arguments); } /** @@ -235,7 +234,7 @@ public final class Local { return; } - error(log, e, e.getMessage(), false); + error(log, e, e.getMessage()); } /** diff --git a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Remote.java b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Remote.java index 13d76a46..98a290ab 100644 --- a/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Remote.java +++ b/easy-retry-common/easy-retry-common-log/src/main/java/com/aizuda/easy/retry/common/log/Remote.java @@ -127,7 +127,7 @@ public final class Remote { return; } - warn(com.aizuda.easy.retry.common.log.LogFactory.get(LogCaller.getCallerCaller()), format, arguments); + warn(LogFactory.get(LogCaller.getCallerCaller()), format, arguments); } /** @@ -143,7 +143,7 @@ public final class Remote { return; } - warn(com.aizuda.easy.retry.common.log.LogFactory.get(LogCaller.getCallerCaller()), e, StrUtil.format(format, arguments)); + warn(LogFactory.get(LogCaller.getCallerCaller()), e, StrUtil.format(format, arguments)); } /** @@ -205,7 +205,7 @@ public final class Remote { return; } - error(com.aizuda.easy.retry.common.log.LogFactory.get(LogCaller.getCallerCaller()), format, arguments); + error(LogFactory.get(LogCaller.getCallerCaller()), format, arguments); } /** @@ -221,7 +221,7 @@ public final class Remote { return; } - error(com.aizuda.easy.retry.common.log.LogFactory.get(LogCaller.getCallerCaller()), e, format, arguments); + error(LogFactory.get(LogCaller.getCallerCaller()), e, format, arguments); } /** @@ -245,7 +245,7 @@ public final class Remote { * @param format 格式文本,{} 代表变量 * @param arguments 变量对应的参数 */ - public void error(com.aizuda.easy.retry.common.log.Log log, String format, Object... arguments) { + public void error(Log log, String format, Object... arguments) { if (!EnvironmentUtils.getLogStatus()) { return; } @@ -261,7 +261,7 @@ public final class Remote { * @param format 格式文本,{} 代表变量 * @param arguments 变量对应的参数 */ - public void error(com.aizuda.easy.retry.common.log.Log log, Throwable e, String format, Object... arguments) { + public void error(Log log, Throwable e, String format, Object... arguments) { if (!EnvironmentUtils.getLogStatus()) { return; } @@ -284,6 +284,6 @@ public final class Remote { return; } - com.aizuda.easy.retry.common.log.LogFactory.get(LogCaller.getCallerCaller()).log(FQCN, level, t, format, true, arguments); + LogFactory.get(LogCaller.getCallerCaller()).log(FQCN, level, t, format, true, arguments); } }