PropKit是非常方便的一个工具类, 但是每次get(xxx)总有些记不住...忍不住使用CV大_法....
既然model都用生成器了...Prop也上生成器吧....
上石马 ~
GeneratorProp.java
package com.momathink.common.generator; import java.util.Properties; import com.jfinal.kit.Kv; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.jfinal.template.Engine; /*** * 根据配置文件 生成 getXxx方法进行获取配置<br> * * @author dufuzhong * * <br> * 2018年5月11日 * */ public class GeneratorProp { static Engine engine = null; static String templateFileName = "/com/momathink/common/generator/template/PropTemplate.html"; protected GeneratorProp init() { if (engine == null) { engine = new Engine("GeneratorProp") .setToClassPathSourceFactory() .addSharedMethod(new Kit()); } return this; } /** * 根据PropKit配置文件, 生成JavaBean * @param fileName = 配置文件名称 (PropKit.use(fileName)) * @param javaPath = 项目JAVA代码地址 * @param packageName = 类的package * @param className = 类的名字 */ public static void generator(String fileName, String javaPath, String packageName, String className) { StringBuilder classPath = new StringBuilder( javaPath.replaceAll("\\\\", "/")) .append("/").append(packageName.replaceAll("\\.", "/")) .append("/").append(className).append(".java"); Prop prop = PropKit.use(fileName); Properties properties = prop.getProperties(); new GeneratorProp() .init() .render(templateFileName, Kv.by("packageName", packageName) .set("className", className) .set("fileName", fileName) .set("properties", properties), classPath); } /** * 根据具体魔板生成文件 * @param templateFileName 模板文件名称 * @param kv 渲染参数 * @param filePath 输出目录 * @return */ public GeneratorProp render(String templateFileName, Kv kv, StringBuilder filePath) { engine.getTemplate(templateFileName).render(kv, filePath.toString()); return this; } //多阅读学习jfinal源码 public static class Kit extends com.jfinal.kit.StrKit{ public static String getJavaType(String v) { if (isBlank(v)) { return "String"; } v = v.trim(); if (isBoolean(v)) { return "Boolean"; } if (isInteger(v)) { return "Integer"; } if (isLong(v)) { return "Long"; } return "String"; } public static String getFnName(String v) { v = toCamelCase(v); v = toCamelCase(v, '.'); v = toCamelCase(v, '-'); v = firstCharToUpperCase(v); return v; } public static boolean isBoolean(String v) { v = v.toLowerCase(); return "true".equals(v) || "false".equals(v); } public static boolean isLong(String v) { try { Long.parseLong(v); return true; } catch (NumberFormatException e) { return false; } } public static boolean isInteger(String v) { try { Integer.parseInt(v); return true; } catch (NumberFormatException e) { return false; } } public static String toCamelCase(String stringWithUnderline, char ch) { if (stringWithUnderline.indexOf(ch) == -1) { return stringWithUnderline; } stringWithUnderline = stringWithUnderline.toLowerCase(); char[] fromArray = stringWithUnderline.toCharArray(); char[] toArray = new char[fromArray.length]; int j = 0; for (int i=0; i<fromArray.length; i++) { if (fromArray[i] == ch) { // 当前字符为指定字符时,将指针后移一位,将紧随指定字符后面一个字符转成大写并存放 i++; if (i < fromArray.length) { toArray[j++] = Character.toUpperCase(fromArray[i]); } } else { toArray[j++] = fromArray[i]; } } return new String(toArray, 0, j); } } }
模版 PropTemplate.html
package #(packageName); import java.util.ArrayList; import java.util.List; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; /** * #(fileName) 配置文件映射JavaBean<br> * @author GeneratorProp配置文件生成器 */ public class #(className) { public static Prop p = PropKit.use("#(fileName)"); private #(className)(){} /** 检查配置文件 是否缺属性, 如果缺则抛 NullPointerException */ public static void check() { String[] keys = { #for(x : properties)"#(x.key)",#end }; int length = keys.length; List<String> retKey = new ArrayList<>(length); for (int i = 0; i < length; i++) { String key = keys[i]; if ( ! containsKey(key)) { retKey.add(key); } } if (retKey.size() > 0) { throw new NullPointerException("#(fileName) 配置文件 缺少属性: " + retKey.toString()); } } #for(x : properties) #set(key = x.key) #set(value = x.value) #set(javaType = getJavaType(value)) #set(fnName = getFnName(key)) /** #(key) */ public static #(javaType) get#(fnName)() { return p.get#(javaType=="String"?"":javaType=="Integer"?"Int":javaType)("#(key)"); } /** #(key) */ public static #(javaType) get#(fnName)(#(javaType) defaultValue) { return p.get#(javaType=="String"?"":javaType=="Integer"?"Int":javaType)("#(key)", defaultValue); } #end public static boolean containsKey(String key) { return p.containsKey(key); } }
测试:
InitConfigGenerator.java
package com.momathink.common.generator; import com.momathink.common.config.YunjiaowuConfig; /** * 更新配置 文件<br> * @author dufuzhong * * <br>2018年5月11日 * */ public class InitConfigGenerator { public static void main(String[] args) { if(YunjiaowuConfig.BOOLEAN) {} GeneratorProp.generator("init.properties", "E:\\workspace\\moma\\jiaowu_ultimate\\src", "com.momathink.common.config", "InitConfig"); } }
嗯, 用起来果然爽多了!
@小99 提出 配置文件初始化属性值,当发现找不到属性Key的时候,会快速失败,不让启动
波总说 这是一个权衡的问题,有些配置是可以不配置的
那就增加一个
check() 方法, 当JFinalConfig启动的时候, 如调取InitConfig.check();就自动检测吧
@JFinal 老大, 这个办法可行 ? 可行就点个赞呗 ~