1. 程式人生 > >利用Freemarker自動生成JavaBean檔案

利用Freemarker自動生成JavaBean檔案

序:

特點:基本上能夠滿足簡單JavaBean的生成,可以通過配置檔案的更改,靈活自動生成對應的bean。

缺點:

1.這裡只是作為一個簡單的例子,在實際應用當中仍需要更改具體的程式碼,以應對不同名稱,不同包名的類,總言之就是配置方面還不夠靈活。

2.對於配置資原始檔的對應引數,雖然很方便,但並沒有對其引數進行嚴格的格式指定,即沒有對應的正則表示式對其獲取的引數進行校驗。因此使用時需較為注意。

正文:

下面直接切入正題:

前提是引入:freemark-xxxx.jar包

/*
  Modification Log:
 -----------------------------------------------------------------------------------
  Version		Notes        		By                Date
  -------   	--------------------------   ---------------------------------------
   1.0			Create       		Techbirds         2012-12-13
 -----------------------------------------------------------------------------------
  */
 package main.classinfo;
 /**
  * 封裝屬性資訊
  *
  */
 public class Attribute {
 	private String name;
 	private String type;
 
 	public Attribute() {
 	}
 
 	public Attribute(String name, String type) {
 		this.name = name;
 		this.type = type;
 	}
 
 	public String getName() {
 		return name;
 	}
 
 	public void setName(String name) {
 		this.name = name;
 	}
 
 	public String getType() {
 		return type;
 	}
 
 	public void setType(String type) {
 		this.type = type;
 	}
 }
 
/*
  Modification Log:
 -----------------------------------------------------------------------------------
  Version		Notes        		By                Date
  -------   	--------------------------   ---------------------------------------
   1.0			Create       		Techbirds         2012-12-13
 -----------------------------------------------------------------------------------
  */
 package main.classinfo;
 /**
  * 封裝類資訊
  *
  */
 public class Class {
 	private String packagename;
 	private String classname;
 	private String[] imports;
 
 	public Class() {
 	}
 
 	public Class(String classname, String[] imports,String packagename) {
 		super();
 		this.classname = classname;
 		this.imports = imports;
 		this.packagename=packagename;
 	}
 
 	public String getClassname() {
 		return classname;
 	}
 
 	public void setClassname(String classname) {
 		this.classname = classname;
 	}
 
 	public String[] getImports() {
 		return imports;
 	}
 
 	public void setImports(String[] imports) {
 		this.imports = imports;
 	}
 
 	public String getPackagename() {
 		return packagename;
 	}
 
 	public void setPackagename(String packagename) {
 		this.packagename = packagename;
 	}
 }
 
public class BeanByCreateMain {
 	private final static String TEMPLATE_NAME="template.ftl";
 	private Configuration cfg;
 	private Properties ploader;
 	/**
 	 * 初始化工作
 	 */
 	public void init() throws Exception {
 		// 負責管理的例項建立+設定模板檔案所在的目錄
 		cfg = new Configuration();
 		// load資源的例項建立
 		ploader = new Properties();
 		cfg.setDirectoryForTemplateLoading(new File(
 				"E:\\Workspaces\\workspace\\javabean_complier\\bean_template"));
 		ploader.load(new FileInputStream(
 						"E:\\Workspaces\\workspace\\javabean_complier\\src\\bean.properties"));
 	}
 
 	
 	public void process() throws IOException, TemplateException {
 		
 		//1.load資訊
 		Class clazz=loadClass();
 		List<Attribute> attributes=loadAttr();
 		//2.向root中放入模版中所需資訊
 		Map<String, Object> root = new HashMap<String, Object>();
 		root.put("clazz",clazz);
 		root.put("attributes",attributes);
 		//3.將模版進行指定檔案的輸出
 		write(root);
 		
 	}
 	/*
 	 * 執行入口
 	 */
 	public static void main(String[] args) throws Exception {
 		BeanByCreateMain hfm = new BeanByCreateMain();
 		hfm.init();
 		hfm.process();
 	}
 	
 	/*
 	 * 載入類資訊:包名,類名,import
 	 */
 	public Class loadClass() {
 		String packagename=ploader.getProperty("packagename");
 		String classname=ploader.getProperty("classname");
 		String values=ploader.getProperty("imports");
 		String[] imports=values.split(",");
 		Class clazz = new Class(classname, imports, packagename);
 		return clazz;
 	}
 	/*
 	 * 載入屬性:屬性名,屬性型別
 	 */
 	public List<Attribute> loadAttr() {
 		List<Attribute> attributes = new ArrayList<Attribute>();
 		String values=ploader.getProperty("attribute");
 		String[] strs=values.split(",");
 		for (String s : strs) {
 			String[] sp=s.split(":");
 			attributes.add(new Attribute(sp[0],sp[1]));
 		}
 		return attributes;
 	}
 	/*
 	 * 將模版進行指定檔案的輸出
 	 */
 	public void write(Map<String, Object> root) throws IOException, TemplateException{
 		Template t = cfg.getTemplate(TEMPLATE_NAME);
 		OutputStream out = new FileOutputStream(
 				new File("E:\\Workspaces\\workspace\\javabean_complier\\src\\bean\\User.java"));
 		t.process(root, new OutputStreamWriter(out));
 	}
 	
 }
 

bean.properties資源檔案

 packagename=bean
 classname=User
 imports=java.util.ArrayList,java.util.List
 attribute=name:String,age:int

template.ftl模版檔案:

package ${clazz.packagename};
 
 <#list clazz.imports as being>
 import ${being};
 </#list>
 
 public class ${clazz.classname} {
 
 <#list attributes as being>
 	private ${being.type} ${being.name};
 	
 	public ${being.type} get${being.name?cap_first}() {
 		return ${being.name};
 	}
 	
 	public void set${being.name?cap_first}(${being.type} ${being.name}) {
 		this.${being.name} = ${being.name};
 	}
 </#list>
執行結果:
package bean;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class User {
 
 	private String name;
 	
 	public String getName() {
 		return name;
 	}
 	
 	public void setName(String name) {
 		this.name = name;
 	}
 	private int age;
 	
 	public int getAge() {
 		return age;
 	}
 	
 	public void setAge(int age) {
 		this.age = age;
 	}
 }