ソースを参照

系统配置移至Core模块,添加系统配置基类以规范配置类的行为

Menethil 7 年 前
コミット
3af6d8c377

+ 4 - 0
litemall-core/pom.xml

@@ -66,6 +66,10 @@
             <artifactId>spring-boot-configuration-processor</artifactId>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>org.linlinjava</groupId>
+            <artifactId>litemall-db</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 3 - 1
litemall-core/src/main/java/org/linlinjava/litemall/core/Application.java

@@ -1,9 +1,11 @@
 package org.linlinjava.litemall.core;
 
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-@SpringBootApplication
+@SpringBootApplication(scanBasePackages = {"org.linlinjava.litemall.core", "org.linlinjava.litemall.db"})
+@MapperScan("org.linlinjava.litemall.db.dao")
 public class Application {
 
     public static void main(String[] args) {

+ 75 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/system/BaseConfig.java

@@ -0,0 +1,75 @@
+package org.linlinjava.litemall.core.system;
+
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * 配置基类,该类实际持有所有的配置,子类只是提供代理访问方法
+ */
+abstract class BaseConfig {
+    //所有的配置均保存在该 HashMap 中
+    protected static Map<String, String> configs = new HashMap<>();
+
+    /**
+     * 子类实现该方法,并告知父类配置前缀,该前缀用来索引配置组用于简化访问和按组重读配置
+     *
+     * @return
+     */
+    abstract String getPrefix();
+
+    /**
+     * 添加配置到公共Map中
+     *
+     * @param key
+     * @param value
+     */
+    public static void addConfig(String key, String value) {
+        configs.put(key, value);
+    }
+
+    /**
+     * 重载配置,传入子类的prefix
+     */
+    public static void reloadConfig(String prefix) {
+        //先遍历删除该 prefix 所有配置
+        for (Iterator<Map.Entry<String, String>> it = configs.entrySet().iterator(); it.hasNext(); ) {
+            Map.Entry<String, String> item = it.next();
+            if (item.getKey().startsWith(prefix))
+                it.remove();
+        }
+
+        ConfigService.getSystemConfigService().reloadConfig(prefix);
+    }
+
+    /**
+     * 按String类型获取配置值
+     *
+     * @param keyName
+     * @return
+     */
+    protected static String getConfig(String keyName) {
+        return configs.get(keyName);
+    }
+
+    /**
+     * 以Integer类型获取配置值
+     *
+     * @param keyName
+     * @return
+     */
+    protected static Integer getConfigInt(String keyName) {
+        return Integer.parseInt(configs.get(keyName));
+    }
+
+    /**
+     * 以BigDecimal类型获取配置值
+     *
+     * @param keyName
+     * @return
+     */
+    protected static BigDecimal getConfigBigDec(String keyName) {
+        return new BigDecimal(configs.get(keyName));
+    }
+}

+ 54 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/system/ConfigService.java

@@ -0,0 +1,54 @@
+package org.linlinjava.litemall.core.system;
+
+import org.linlinjava.litemall.db.domain.LitemallSystem;
+import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.List;
+
+/**
+ * 该类用于自动初始化数据库配置到BaseConfig中,以便BaseConfig的子类调用
+ */
+@Component
+class ConfigService {
+    private static ConfigService systemConfigService;
+
+    static ConfigService getSystemConfigService() {
+        return systemConfigService;
+    }
+
+    @Autowired
+    private LitemallSystemConfigService litemallSystemConfigService;
+
+    //不允许实例化
+    private ConfigService() {
+
+    }
+
+    @PostConstruct
+    public void inist() {
+        systemConfigService = this;
+        systemConfigService.inistConfigs();
+    }
+
+    public void reloadConfig(String prefix) {
+        List<LitemallSystem> list = litemallSystemConfigService.queryAll();
+        for (LitemallSystem item : list) {
+            //符合条件,添加
+            if (item.getKeyName().startsWith(prefix))
+                BaseConfig.addConfig(item.getKeyName(), item.getKeyValue());
+        }
+    }
+
+    /**
+     * 读取全部配置
+     */
+    private void inistConfigs() {
+        List<LitemallSystem> list = litemallSystemConfigService.queryAll();
+        for (LitemallSystem item : list) {
+            BaseConfig.addConfig(item.getKeyName(), item.getKeyValue());
+        }
+    }
+}

+ 64 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/system/SystemConfig.java

@@ -0,0 +1,64 @@
+package org.linlinjava.litemall.core.system;
+
+import java.math.BigDecimal;
+
+/**
+ * 系统设置
+ */
+
+public class SystemConfig extends BaseConfig {
+    public static final String PRE_FIX = "litemall.system.";
+
+    public static Integer getNewLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.new");
+    }
+
+    public static Integer getHotLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.hot");
+    }
+
+    public static Integer getBrandLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.brand");
+    }
+
+    public static Integer getTopicLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.topic");
+    }
+
+    public static Integer getCatlogListLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.catloglist");
+    }
+
+    public static Integer getCatlogMoreLimit() {
+        return getConfigInt(PRE_FIX + "indexlimit.catloggood");
+    }
+
+    public static String getHotBannerTitle() {
+        return getConfig(PRE_FIX + "banner.hot.title");
+    }
+
+    public static String getNewBannerTitle() {
+        return getConfig(PRE_FIX + "banner.new.title");
+    }
+
+    public static String getHotImageUrl() {
+        return getConfig(PRE_FIX + "banner.hot.imageurl");
+    }
+
+    public static String getNewImageUrl() {
+        return getConfig(PRE_FIX + "banner.new.imageurl");
+    }
+
+    public static BigDecimal getFreight() {
+        return getConfigBigDec(PRE_FIX + "freight.value");
+    }
+
+    public static BigDecimal getFreightLimit() {
+        return getConfigBigDec(PRE_FIX + "freight.limit");
+    }
+
+    @Override
+    public String getPrefix() {
+        return PRE_FIX;
+    }
+}

+ 0 - 79
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/ConfigService.java

@@ -1,79 +0,0 @@
-package org.linlinjava.litemall.wx.service;
-
-import org.linlinjava.litemall.db.domain.LitemallSystem;
-import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import javax.annotation.PostConstruct;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-@Component
-public class ConfigService {
-    private static ConfigService systemConfigService;
-
-    //系统配置组
-    private Map<String, String> systemConfig;
-
-    @Autowired
-    private LitemallSystemConfigService litemallSystemConfigService;
-
-    //不允许实例化
-    private ConfigService() {
-
-    }
-
-    /**
-     * 获取系统配置
-     *
-     * @param keyName 例如 : litemall.system.freight.value
-     * @return 返回该值的字符串
-     */
-    public String getSysValue(String keyName) {
-        keyName = "litemall.system." + keyName;
-        return systemConfig.get(keyName);
-    }
-
-    /**
-     * 获取系统配置数值型
-     *
-     * @param keyName 例如 : litemall.system.freight.value
-     * @return 返回该值的字符串
-     */
-    public Integer getSysValueInt(String keyName) {
-        keyName = "litemall.system." + keyName;
-        return Integer.parseInt(systemConfig.get(keyName));
-    }
-
-
-    /**
-     * 获取系统设置实例
-     *
-     * @return
-     */
-    public static ConfigService getCfg() {
-        return systemConfigService;
-    }
-
-
-    @PostConstruct
-    public void inist() {
-        systemConfigService = this;
-        systemConfigService.systemConfig = new HashMap<>();
-        systemConfigService.inistConfigs();
-    }
-
-    /**
-     * 读取全部配置
-     */
-    private void inistConfigs() {
-        List<LitemallSystem> list = litemallSystemConfigService.queryAll();
-        for (LitemallSystem item : list) {
-            //属于系统配置,放置到系统配置组
-            if (item.getKeyName().startsWith("litemall.system"))
-                systemConfig.put(item.getKeyName(), item.getKeyValue());
-        }
-    }
-}

+ 0 - 57
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/SystemConfig.java

@@ -1,57 +0,0 @@
-package org.linlinjava.litemall.wx.service;
-
-import java.math.BigDecimal;
-
-/**
- * 系统设置
- */
-
-public class SystemConfig {
-    public static Integer getNewLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.new");
-    }
-
-    public static Integer getHotLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.hot");
-    }
-
-    public static Integer getBrandLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.brand");
-    }
-
-    public static Integer getTopicLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.topic");
-    }
-
-    public static Integer getCatlogListLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.catloglist");
-    }
-
-    public static Integer getCatlogMoreLimit() {
-        return ConfigService.getCfg().getSysValueInt("indexlimit.catloggood");
-    }
-
-    public static String getHotBannerTitle() {
-        return ConfigService.getCfg().getSysValue("banner.hot.title");
-    }
-
-    public static String getNewBannerTitle() {
-        return ConfigService.getCfg().getSysValue("banner.new.title");
-    }
-
-    public static String getHotImageUrl() {
-        return ConfigService.getCfg().getSysValue("banner.hot.imageurl");
-    }
-
-    public static String getNewImageUrl() {
-        return ConfigService.getCfg().getSysValue("banner.new.imageurl");
-    }
-
-    public static BigDecimal getFreight() {
-        return new BigDecimal(ConfigService.getCfg().getSysValue("freight.value"));
-    }
-
-    public static BigDecimal getFreightLimit() {
-        return new BigDecimal(ConfigService.getCfg().getSysValue("freight.limit"));
-    }
-}

+ 1 - 1
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxCartController.java

@@ -8,7 +8,7 @@ import org.linlinjava.litemall.db.service.*;
 import org.linlinjava.litemall.core.util.JacksonUtil;
 import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.wx.annotation.LoginUser;
-import org.linlinjava.litemall.wx.service.SystemConfig;
+import org.linlinjava.litemall.core.system.SystemConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 

+ 1 - 1
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxGoodsController.java

@@ -7,7 +7,7 @@ import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.db.domain.*;
 import org.linlinjava.litemall.db.service.*;
 import org.linlinjava.litemall.wx.annotation.LoginUser;
-import org.linlinjava.litemall.wx.service.SystemConfig;
+import org.linlinjava.litemall.core.system.SystemConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;

+ 1 - 1
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxHomeController.java

@@ -5,7 +5,7 @@ import org.apache.commons.logging.LogFactory;
 import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.db.domain.*;
 import org.linlinjava.litemall.db.service.*;
-import org.linlinjava.litemall.wx.service.SystemConfig;
+import org.linlinjava.litemall.core.system.SystemConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;

+ 1 - 1
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxOrderController.java

@@ -18,7 +18,7 @@ import org.linlinjava.litemall.db.service.*;
 import org.linlinjava.litemall.db.util.OrderHandleOption;
 import org.linlinjava.litemall.db.util.OrderUtil;
 import org.linlinjava.litemall.wx.annotation.LoginUser;
-import org.linlinjava.litemall.wx.service.SystemConfig;
+import org.linlinjava.litemall.core.system.SystemConfig;
 import org.linlinjava.litemall.wx.util.IpUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.PlatformTransactionManager;