فهرست منبع

1、初始化配置加载优化

yys 6 روز پیش
والد
کامیت
6d03b05483

+ 0 - 47
fs-admin/src/main/java/com/fs/admin/config/StubWxPayConfiguration.java

@@ -1,47 +0,0 @@
-package com.fs.admin.config;
-
-import com.fs.core.config.WxPayProperties;
-import com.github.binarywang.wxpay.service.WxPayService;
-import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * fs-admin 模块的微信支付 Stub 配置
- * <p>
- * 仅在 fs.startup.init-wechat-config=false 时激活(即 fs-admin),
- * 提供不查 DB 的 WxPayService 和 WxPayProperties,避免启动时查 DB。
- * 其他模块 matchIfMissing=true,不会加载此 Stub。
- *
- * @author fs
- * @date 2026-06-01
- */
-@Slf4j
-@Configuration
-@ConditionalOnProperty(prefix = "fs.startup", name = "init-wechat-config", havingValue = "false")
-public class StubWxPayConfiguration {
-
-    /**
-     * 空的微信支付服务,不连接任何支付通道
-     */
-    @Bean
-    @ConditionalOnMissingBean
-    public WxPayService wxService() {
-        log.info("[fs-admin] 创建空的微信支付服务(Stub)");
-        return new WxPayServiceImpl();
-    }
-
-    /**
-     * 微信支付配置 Stub,不会在启动时查询数据库。
-     * 手动注入 SysConfigMapper 和 RedisCache,确保运行时调用 getter 不 NPE。
-     */
-    @Bean
-    @ConditionalOnMissingBean
-    public WxPayProperties wxPayProperties() {
-        log.info("[fs-admin] 创建微信支付配置(Stub,不查 DB)");
-        return new WxPayProperties();
-    }
-}

+ 8 - 0
fs-service/src/main/java/com/fs/core/config/RedissonShutdownListener.java

@@ -2,6 +2,7 @@ package com.fs.core.config;
 
 
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.redisson.api.RedissonClient;
 import org.redisson.api.RedissonClient;
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 
 
@@ -25,6 +26,10 @@ public class RedissonShutdownListener implements ServletContextListener {
             if (ctx == null) {
             if (ctx == null) {
                 return;
                 return;
             }
             }
+            if (ctx instanceof ConfigurableApplicationContext
+                    && !((ConfigurableApplicationContext) ctx).isActive()) {
+                return;
+            }
             if (!ctx.containsBean("redissonClient")) {
             if (!ctx.containsBean("redissonClient")) {
                 return;
                 return;
             }
             }
@@ -34,6 +39,9 @@ public class RedissonShutdownListener implements ServletContextListener {
                 client.shutdown();
                 client.shutdown();
                 Thread.sleep(SHUTDOWN_WAIT_MS);
                 Thread.sleep(SHUTDOWN_WAIT_MS);
             }
             }
+        } catch (IllegalStateException e) {
+            // 启动失败时 Spring 上下文尚未 refresh 完成,无需关闭 Redisson
+            log.debug("[Redisson] 上下文未就绪,跳过关闭: {}", e.getMessage());
         } catch (InterruptedException e) {
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
             Thread.currentThread().interrupt();
             log.warn("[Redisson] 等待关闭时被中断", e);
             log.warn("[Redisson] 等待关闭时被中断", e);

+ 31 - 0
fs-service/src/main/java/com/fs/core/config/StubWxPayConfiguration.java

@@ -0,0 +1,31 @@
+package com.fs.core.config;
+
+import com.github.binarywang.wxpay.service.WxPayService;
+import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 未启用 {@code fs.wxpay.enabled=true} 时提供占位 Bean,避免各启动模块重复配置 Stub。
+ * 真实配置见 {@link WxPayConfiguration}。
+ */
+@Slf4j
+@Configuration
+public class StubWxPayConfiguration {
+
+    @Bean
+    @ConditionalOnMissingBean(WxPayService.class)
+    public WxPayService wxService() {
+        log.debug("创建微信支付 Stub 服务(未启用 fs.wxpay.enabled)");
+        return new WxPayServiceImpl();
+    }
+
+    @Bean
+    @ConditionalOnMissingBean(WxPayProperties.class)
+    public WxPayProperties wxPayProperties() {
+        log.debug("创建微信支付配置 Stub(不查库)");
+        return new WxPayProperties();
+    }
+}

+ 1 - 1
fs-service/src/main/java/com/fs/core/config/WxPayConfiguration.java

@@ -16,7 +16,7 @@ import org.springframework.context.annotation.Scope;
 @Slf4j
 @Slf4j
 @Configuration
 @Configuration
 @ConditionalOnClass(WxPayService.class)
 @ConditionalOnClass(WxPayService.class)
-@ConditionalOnProperty(prefix = "fs.startup", name = "init-wechat-config", havingValue = "true", matchIfMissing = true)
+@ConditionalOnProperty(prefix = "fs.wxpay", name = "enabled", havingValue = "true", matchIfMissing = false)
 @AllArgsConstructor
 @AllArgsConstructor
 public class WxPayConfiguration {
 public class WxPayConfiguration {
   private WxPayProperties properties;
   private WxPayProperties properties;

+ 17 - 3
fs-service/src/main/java/com/fs/core/config/WxPayProperties.java

@@ -9,15 +9,20 @@ import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 
 @Component
 @Component
 @Data
 @Data
 @Slf4j
 @Slf4j
+@ConditionalOnProperty(prefix = "fs.wxpay", name = "enabled", havingValue = "true", matchIfMissing = false)
 public class WxPayProperties {
 public class WxPayProperties {
 
 
+  private static final AtomicBoolean MISSING_CONFIG_WARNED = new AtomicBoolean(false);
+
   @Autowired
   @Autowired
   private SysConfigMapper sysConfigMapper;
   private SysConfigMapper sysConfigMapper;
   @Autowired
   @Autowired
@@ -33,6 +38,9 @@ public class WxPayProperties {
    * 公用方法:查询数据库获取微信支付配置
    * 公用方法:查询数据库获取微信支付配置
    */
    */
   private ProjectConfig.Wx.Pay getWxPayConfigFromDB() {
   private ProjectConfig.Wx.Pay getWxPayConfigFromDB() {
+    if (sysConfigMapper == null) {
+      return null;
+    }
     try {
     try {
       // 1. 先从Redis缓存中获取
       // 1. 先从Redis缓存中获取
       ProjectConfig cachedConfig = getConfigFromCache();
       ProjectConfig cachedConfig = getConfigFromCache();
@@ -45,18 +53,24 @@ public class WxPayProperties {
 
 
       ProjectConfig projectConfig = ProjectConfig.getFromDB(sysConfigMapper);
       ProjectConfig projectConfig = ProjectConfig.getFromDB(sysConfigMapper);
       if (projectConfig == null) {
       if (projectConfig == null) {
-        log.warn("未找到微信支付配置");
+        if (MISSING_CONFIG_WARNED.compareAndSet(false, true)) {
+          log.debug("未找到微信支付配置");
+        }
         return null;
         return null;
       }
       }
 
 
       if (projectConfig.getWx() == null) {
       if (projectConfig.getWx() == null) {
-        log.warn("配置中未找到wx节点");
+        if (MISSING_CONFIG_WARNED.compareAndSet(false, true)) {
+          log.debug("配置中未找到wx节点");
+        }
         return null;
         return null;
       }
       }
 
 
       ProjectConfig.Wx.Pay payConfig = projectConfig.getWx().getPay();
       ProjectConfig.Wx.Pay payConfig = projectConfig.getWx().getPay();
       if (payConfig == null) {
       if (payConfig == null) {
-        log.warn("配置中未找到pay节点");
+        if (MISSING_CONFIG_WARNED.compareAndSet(false, true)) {
+          log.debug("配置中未找到pay节点");
+        }
         return null;
         return null;
       }
       }
 
 

+ 5 - 0
fs-user-app/src/main/resources/application.yml

@@ -17,3 +17,8 @@ spring:
 #    active: druid-qdtst
 #    active: druid-qdtst
 #    active: druid-yzt
 #    active: druid-yzt
 #    active: druid-bjzm-test
 #    active: druid-bjzm-test
+
+# 微信支付(仅 fs-user-app 启用,见 WxPayConfiguration)
+fs:
+  wxpay:
+    enabled: true