woody il y a 4 mois
Parent
commit
40216cd4de

+ 4 - 4
framework-base/src/main/java/com/chelvc/framework/base/context/JacksonContextHolder.java

@@ -6,6 +6,7 @@ import java.io.OutputStream;
 import java.lang.reflect.Type;
 
 import com.chelvc.framework.common.util.JacksonUtils;
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.NonNull;
@@ -33,13 +34,12 @@ public final class JacksonContextHolder {
     public static ObjectMapper getGlobalSerializer() {
         if (GLOBAL_SERIALIZER == null) {
             synchronized (ObjectMapper.class) {
-                if (GLOBAL_SERIALIZER == null
-                        && (GLOBAL_SERIALIZER = ApplicationContextHolder.getBean(ObjectMapper.class, false)) == null) {
-                    GLOBAL_SERIALIZER = new ObjectMapper();
+                if (GLOBAL_SERIALIZER == null) {
+                    GLOBAL_SERIALIZER = ApplicationContextHolder.getBean(ObjectMapper.class, false);
                 }
             }
         }
-        return GLOBAL_SERIALIZER;
+        return ObjectUtils.ifNull(GLOBAL_SERIALIZER, JacksonUtils::getDefaultSerializer);
     }
 
     /**

+ 30 - 10
framework-base/src/main/java/com/chelvc/framework/base/context/RestContextHolder.java

@@ -25,28 +25,48 @@ import org.springframework.web.client.RestTemplate;
 @Slf4j
 public final class RestContextHolder {
     /**
-     * RestTemplate实例
+     * 全局RestTemplate实例
      */
-    private static volatile RestTemplate TEMPLATE;
+    private static volatile RestTemplate GLOBAL_TEMPLATE;
+
+    /**
+     * 默认RestTemplate实例
+     */
+    private static volatile RestTemplate DEFAULT_TEMPLATE;
 
     private RestContextHolder() {
     }
 
     /**
-     * 获取RestTemplate实例
+     * 获取全局RestTemplate实例
+     *
+     * @return RestTemplate实例
+     */
+    public static RestTemplate getGlobalTemplate() {
+        if (GLOBAL_TEMPLATE == null) {
+            synchronized (RestTemplate.class) {
+                if (GLOBAL_TEMPLATE == null) {
+                    GLOBAL_TEMPLATE = ApplicationContextHolder.getBean(RestTemplate.class, false);
+                }
+            }
+        }
+        return ObjectUtils.ifNull(GLOBAL_TEMPLATE, RestContextHolder::getDefaultTemplate);
+    }
+
+    /**
+     * 获取默认RestTemplate实例
      *
      * @return RestTemplate实例
      */
-    public static RestTemplate getTemplate() {
-        if (TEMPLATE == null) {
+    public static RestTemplate getDefaultTemplate() {
+        if (DEFAULT_TEMPLATE == null) {
             synchronized (RestTemplate.class) {
-                if (TEMPLATE == null
-                        && (TEMPLATE = ApplicationContextHolder.getBean(RestTemplate.class, false)) == null) {
-                    TEMPLATE = new RestTemplate();
+                if (DEFAULT_TEMPLATE == null) {
+                    DEFAULT_TEMPLATE = new RestTemplate();
                 }
             }
         }
-        return TEMPLATE;
+        return DEFAULT_TEMPLATE;
     }
 
     /**
@@ -58,7 +78,7 @@ public final class RestContextHolder {
      */
     public static <T> T execute(@NonNull Function<RestTemplate, T> function) {
         try {
-            return function.apply(getTemplate());
+            return function.apply(getGlobalTemplate());
         } catch (Exception e) {
             if (ErrorUtils.contains(e, IOException.class)) {
                 log.warn("Rest invoke failed and retry later: {}", e.getMessage());