为什么要分离?

业务需求是不停地变,如果把条件写进代码里,当用户需求变时要改代码发版本更新才能生效,这过程无疑是漫长的

就算是在开发期,不停的变开发者精力耗光在沟通,小修改上,无法专注逻辑部分

分离的根本目的是让开发者专注写引擎部分,无需关注太多业务上的边界,条件等

 

需要分离什么类型数值?

如活动开启时间,购买满足条件,购买上限等 这些不确定用户具体需求,全都可以弄成动态获取

 

分离技术实现有很多

如使用数据库mysql等

linux 常用的配置文本config

表格csv,json文件等

本项目用的是 java Properties 跟单例

  1 import java.io.FileInputStream;
  2 import java.io.InputStream;
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.Method;
  5 import java.lang.reflect.Type;
  6 import java.util.Collection;
  7 import java.util.Collections;
  8 import java.util.HashSet;
  9 import java.util.Map;
 10 import java.util.Properties;
 11 import java.util.Set;
 12 
 13 import javax.script.ScriptEngine;
 14 import javax.script.ScriptEngineManager;
 15 
 16 import org.apache.commons.lang3.reflect.TypeUtils;
 17 
 18 import com.eyu.onequeue.reflect.anno.FieldValue;
 19 import com.eyu.onequeue.util.SerialUtil;
 20 
 21 /**
 22  * @author solq
 23  */
 24 public class PropertiesFactory {
 25     static final ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
 26 
 27     public static <T> T initField(Class<T> clz, String file) {
 28     T ret = null;
 29     try {
 30         Properties pro = getProperties(file);
 31         ret = clz.newInstance();
 32         Field[] fields = clz.getDeclaredFields();
 33         Set<Method> methods = new HashSet<>();
 34         Collections.addAll(methods, clz.getDeclaredMethods());
 35         // Field modifiersField = Field.class.getDeclaredField("modifiers");
 36         // modifiersField.setAccessible(true);
 37         for (Field f : fields) {
 38         FieldValue anno = f.getAnnotation(FieldValue.class);
 39         if (anno == null) {
 40             continue;
 41         }
 42 
 43         String fileName = anno.value();
 44         String tmp = (String) pro.get(fileName);
 45         if (tmp == null) {
 46             continue;
 47         }
 48         Object value = tmp;
 49         Type type = f.getGenericType();
 50         if (type.equals(Integer.TYPE)) {
 51             value = se.eval(tmp);
 52         } else if (type.equals(Double.TYPE)) {
 53             value = se.eval(tmp);
 54         } else if (type.equals(Float.TYPE)) {
 55             value = se.eval(tmp);
 56         } else if (type.equals(Long.TYPE)) {
 57             value = se.eval(tmp);
 58         } else if (type.equals(Short.TYPE)) {
 59             value = ((Integer) se.eval(tmp)).shortValue();
 60         } else if (type.equals(Byte.TYPE)) {
 61             value = ((Integer) se.eval(tmp)).byteValue();
 62         } else if (type.equals(Boolean.TYPE)) {
 63             value = se.eval(tmp);
 64         } else if (TypeUtils.isAssignable(type, Map.class)) {
 65             value = SerialUtil.readValue(tmp, type);
 66         }else if (TypeUtils.isAssignable(type, Collection.class)) {
 67             value = SerialUtil.readValue(tmp, type);
 68         }else if (TypeUtils.isAssignable(type, Class.class)) {
 69             value = Class.forName(tmp);
 70         }else if (TypeUtils.isArrayType(type)) {
 71             value = SerialUtil.readArray(tmp, type);
 72         }
 73          
 74         String callMethodName = "set" + f.getName();
 75         boolean flag = false;
 76         try {
 77             for (Method m : methods) {
 78             if (m.getName().equals(callMethodName)) {
 79                 m.setAccessible(true);
 80                 m.invoke(ret, value);
 81                 flag = true;
 82                 break;
 83             }
 84             }
 85         } catch (Exception e) {
 86 
 87         }
 88         if (flag) {
 89             continue;
 90         }
 91         f.setAccessible(true);
 92         // modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
 93 
 94         try {
 95             f.set(ret, value);
 96         } catch (Exception e1) {
 97             e1.printStackTrace();
 98         }
 99         }
100     } catch (Exception e1) {
101         e1.printStackTrace();
102     }
103     return ret;
104     }
105 
106     public static Properties getProperties(String file) {
107     Properties pro = new Properties();
108     String path = ClassLoader.getSystemResource(file).getPath();
109     try (InputStream fs = new FileInputStream(path);) {
110         pro.load(fs);
111     } catch (Exception e) {
112         e.printStackTrace();
113     }
114     return pro;
115     }
116 }
PropertiesFactory

相关文章:

  • 2021-07-29
  • 2021-09-25
  • 2021-06-20
  • 2021-11-24
  • 2021-07-17
  • 2022-02-08
  • 2022-02-16
  • 2021-09-15
猜你喜欢
  • 2021-05-29
  • 2021-10-30
  • 2021-08-24
  • 2022-02-27
  • 2021-11-13
  • 2021-08-29
相关资源
相似解决方案