刚看见站内一篇文章Eclipse使用技巧,于是想把这篇文章说得不的代码模板使用技巧总结下。
我要说的java代码模板是Windows->Preferences->Java->Editor->Templates中设置的模板
如果你不在得到什么是eclipse的代码模板,去问google就知道了~~很实用的功能哦!
我搜索到的:eclispe template用法 图文并茂,很适合初学者。
1. 默认的模板template
在templates面板里已经有许多默认的模板,不熟悉的朋友可以自己研究一下。
比如:输入sysout,然后Shift+SPACE确定出现下面的代码
System.out.println(); |
说的不够详细,下次我写过图文的说明~~
Template:Alt + /
修改处:Window->Preference->Workbench->Keys->Command->Edit->Content Assist。
个人习惯:Shift+SPACE(空白)。
2. 默认的模板template
在templates面板里已经有许多默认的模板,不熟悉的朋友可以自己研究一下。
比如:输入sysout,然后Shift+SPACE确定出现下面的代码
System.out.println(); |
说的不够详细,下次我写过图文的说明~~
<!--EndFragment-->
3.下面都是自定义的模板
name:getlog
private static final Log logger =LogFactory.getLog( ${enclosing_type}.class ); |
如果你希望把import也添加进去,比如使用apache的log则把模板改为:
${:import(org.apache.commons.logging.Log, org.apache.commons.logging.LogFactory)} private static final Log logger = LogFactory.getLog(${enclosing_type}.class); |
name:debug
if (logger.isDebugEnabled()) { logger.debug(${Message}, ${exception}); } |
name:error
logger.error(${Message}, ${exception}); |
name:info
logger.info(${Message}, ${exception}); |
name:warn
logger.warn(${Message}, ${exception}); |
name:const 常量
private static final ${type} ${name} = new ${type} ${cursor}; |
读文件的模板readfile
${:import(java.io.BufferedReader, java.io.FileNotFoundException, java.io.FileReader, java.io.IOException)} BufferedReader in = null; try { in = new BufferedReader(new FileReader(${fileName})); String line; while ((line = in.readLine()) != null) { ${process} } } catch (FileNotFoundException e) { logger.error(e) ; } catch (IOException e) { logger.error(e) ; } finally { if(in != null) in.close(); } ${cursor} |
单例模板singleton设计模式:
private static Object syncObj = new Object(); //使用了一个私有静态变量syncObj来保证线程同步 private static RuleManager instance; private RuleManager() { this.init(); } public static RuleManager getInstance(){ if(instance != null){ return instance; } synchronized (syncObj){ if(instance != null) { return instance; } instance = new RuleManager(); } return instance; } ${cursor} |
${cursor}:鼠标的位置
也可以根据自己的需要设置各种不同的代码模板,方便快速开发。
还有些模板比如连数据库、其它一些固定模式的代码生成模板就不一一贴出来了~~~
<!--EndFragment-->