Java properties file to Constant class
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/main
.gitignore
README.md
build.gradle

README.md

constant-generator

Java常量类生成器

  • 需求
  1. properties配置文件,创建常量类
  2. 常量类创建后,繁多设置属性值
  3. 配置文件变更和常量类不同步,容易遗漏
  • 特点
  1. 支持自动生成常量类 ConfigGenerator
  2. 支持无限树形结构生成,基础数据类型(默认String,自定义属性type)
  3. 支持自动设置值 ConfigLoader
  • 作用:只需维护properties文件,自动生成类和设置属性值

示例

config.properties 配置文件

name=linsage
devMode=true
devMode.type=boolean
#数据库
db.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
db.user=root
db.password=123456
#druid
druid.initialSize=10
druid.minIdle=10
druid.maxActive=100
#监控页面
druid.visitPath=/zs
#慢sql
druid.logSlowSql=true
druid.slowSqlMillis=100
#qiuniu
qiniu.accessKey=123456
qiniu.secretKey=56788
qiniu.separator=_
qiniu.bucket=test1
qiniu.domain=http://1.bkt.clouddn.com
qiniu.ins.bucket=test2
qiniu.ins.domain=http://2.bkt.clouddn.com
#proxy
httpProxy.ip=127.0.0.1
httpProxy.port=8118
httpProxy.port.type=int

ConfigGenerator 配置文件生成常量类

public static void main(String[] args) {
    /**
     * 生成常量类
     */
    // base model 文件保存路径
    String outputDir = PathKit.getWebRootPath() + "/../src/main/java/xyz/linsage/model";
    //包名
    String packageName = "xyz.linsage.model";
    //类名
    String className = "Constant";
    ConfigGenerator g = new ConfigGenerator(packageName, className, outputDir);
    //带顺序,为了和配置文件一致
    LinkedProp prop = LinkedPropKit.use("config.properties");
    g.generate(prop.getProperties());
}

生成类

package xyz.linsage.model;

/**
 * Generated by linsage, do not modify this file.
 */
@SuppressWarnings("serial")
public  class Constant {
	 public static String name;
	 public static boolean devMode;

	 public static class db {
		 public static String url;
		 public static String user;
		 public static String password;
	}

	 public static class druid {
		 public static String initialSize;
		 public static String minIdle;
		 public static String maxActive;
		 public static String visitPath;
		 public static String logSlowSql;
		 public static String slowSqlMillis;
	}

	 public static class qiniu {
		 public static String accessKey;
		 public static String secretKey;
		 public static String separator;
		 public static String bucket;
		 public static String domain;

		 public static class ins {
			 public static String bucket;
			 public static String domain;
		}
	}

	 public static class httpProxy {
		 public static String ip;
		 public static int port;
	}
}

ConfigLoader 自动加载配置文件属性值

public static void main(String[] args) {
    LinkedProp prop = LinkedPropKit.use("config.properties");
    ConfigLoader configLoader = new ConfigLoader();
    configLoader.load(Constant.class, prop.getProperties());

    System.out.println(Constant.name);
    System.out.println(Constant.devMode);
    System.out.println(Constant.db.url);
    System.out.println(Constant.qiniu.ins.domain);
    System.out.println(Constant.httpProxy.port);
    System.out.println("ok");
}