Commit 3af6d8c3 authored by Menethil's avatar Menethil
Browse files

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

parent 0e62f33e
......@@ -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>
......
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) {
......
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));
}
}
package org.linlinjava.litemall.wx.service;
package org.linlinjava.litemall.core.system;
import org.linlinjava.litemall.db.domain.LitemallSystem;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
......@@ -6,16 +6,18 @@ 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;
/**
* 该类用于自动初始化数据库配置到BaseConfig中,以便BaseConfig的子类调用
*/
@Component
public class ConfigService {
class ConfigService {
private static ConfigService systemConfigService;
//系统配置组
private Map<String, String> systemConfig;
static ConfigService getSystemConfigService() {
return systemConfigService;
}
@Autowired
private LitemallSystemConfigService litemallSystemConfigService;
......@@ -25,55 +27,28 @@ public class 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();
}
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) {
//属于系统配置,放置到系统配置组
if (item.getKeyName().startsWith("litemall.system"))
systemConfig.put(item.getKeyName(), item.getKeyValue());
BaseConfig.addConfig(item.getKeyName(), item.getKeyValue());
}
}
}
}
\ No newline at end of file
package org.linlinjava.litemall.wx.service;
package org.linlinjava.litemall.core.system;
import java.math.BigDecimal;
......@@ -6,52 +6,59 @@ import java.math.BigDecimal;
* 系统设置
*/
public class SystemConfig {
public class SystemConfig extends BaseConfig {
public static final String PRE_FIX = "litemall.system.";
public static Integer getNewLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.new");
return getConfigInt(PRE_FIX + "indexlimit.new");
}
public static Integer getHotLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.hot");
return getConfigInt(PRE_FIX + "indexlimit.hot");
}
public static Integer getBrandLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.brand");
return getConfigInt(PRE_FIX + "indexlimit.brand");
}
public static Integer getTopicLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.topic");
return getConfigInt(PRE_FIX + "indexlimit.topic");
}
public static Integer getCatlogListLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.catloglist");
return getConfigInt(PRE_FIX + "indexlimit.catloglist");
}
public static Integer getCatlogMoreLimit() {
return ConfigService.getCfg().getSysValueInt("indexlimit.catloggood");
return getConfigInt(PRE_FIX + "indexlimit.catloggood");
}
public static String getHotBannerTitle() {
return ConfigService.getCfg().getSysValue("banner.hot.title");
return getConfig(PRE_FIX + "banner.hot.title");
}
public static String getNewBannerTitle() {
return ConfigService.getCfg().getSysValue("banner.new.title");
return getConfig(PRE_FIX + "banner.new.title");
}
public static String getHotImageUrl() {
return ConfigService.getCfg().getSysValue("banner.hot.imageurl");
return getConfig(PRE_FIX + "banner.hot.imageurl");
}
public static String getNewImageUrl() {
return ConfigService.getCfg().getSysValue("banner.new.imageurl");
return getConfig(PRE_FIX + "banner.new.imageurl");
}
public static BigDecimal getFreight() {
return new BigDecimal(ConfigService.getCfg().getSysValue("freight.value"));
return getConfigBigDec(PRE_FIX + "freight.value");
}
public static BigDecimal getFreightLimit() {
return new BigDecimal(ConfigService.getCfg().getSysValue("freight.limit"));
return getConfigBigDec(PRE_FIX + "freight.limit");
}
@Override
public String getPrefix() {
return PRE_FIX;
}
}
}
\ No newline at end of file
......@@ -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.*;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment