使用 Lombok 简化项目中无谓的Java代码
在写使用Java时,难免会有一些模板代码要写,不然get/set,toString, hashCode, close 资源,定义构造函数等等。代码会显得很冗余,很长。Lombok项目可以是我们摆脱这些东西,通过一系列的注解,Lombok可以帮我们自动生成这些函数。
Lombok 官网地址:https://projectlombok.org/
参考文档:https://projectlombok.org/features/index.html
1. 安装
到官网下载 lombok.jar,直接双击,按照提示进行操作,就可以在eclipse中安装成功。
如果使用maven时,则需要引入依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
<scope>provided</scope>
</dependency>
如果需要用javac或者其他命令工具编译java类,则需要将 lombok.jar放入classpath.
2. 使用方法 (文档:https://projectlombok.org/features/index.html)
1> @Getter/@Setter, 注解在一个pojo类上,会在编译时,帮我们自动生成get/set函数。
2> @ToString 注解在类上,编译时,帮我们生成包括所有field的toString函数;
3> @EqualsAndHashCode, 编译时,帮我们生成equlas 和hashCode函数;
4> @Cleanup, 注解在一些资源对象的定义上,可以帮我们自动调用它们的close()函数;这个很有帮助;
5> @NoArgsContructor,@RequireArgsContructor, @AllArgsContructor,分别帮我们生成无参数构造函数,每一个非Null的field的构造函数,所有field参数的构造函数;
6> @Data,All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor! (等价于:@ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor)
更多的注解,参见https://projectlombok.org/features/index.html
3. 例子
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Test {
private int id;
private String name;
private String password; public static void main(String[] args) {
Test test = new Test(1, "test", "password");
System.out.println(test);
System.out.println(test.getName());
}
}
结果:
Test(id=1, name=test, password=password)
test
通过@Data, @AllArgsConstructor,@NoArgsConstructor 三个注解自动 生成了 Test 的全field参数的构造函数,自动生成了 toString(), get/set函数等等。
再看一例:
public static void main(String[] args) throws IOException{
@Cleanup
InputStream in = new FileInputStream("/home/a.txt");
@Cleanup
OutputStream out = new FileOutputStream("/home/b.txt");
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1)
break;
out.write(b, 0, r);
}
}
@Cleanup自动帮我们调用 close() 方法进行关闭资源。
You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a
try/finally construct.
If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can
specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via
@Cleanup.
@Cleanup是通过 try/finally 实现的,如果资源的关闭方法不是默认的close(),那么也可以指定关闭方法的名称@Cleanup("closeMethod"), 但是关闭方法不能有参数,不然就无法使用 @Cleanup了。
更多的 参考 https://projectlombok.org/features/index.html
通过使用 Lombok,可以减少很多的 Java 代码的,减轻了心理负担。
使用 Lombok 简化项目中无谓的Java代码的更多相关文章
- VS Code项目中共享自定义的代码片段方案
VS Code项目中共享自定义的代码片段方案 一.问题背景 项目中注释风格不统一,如何统一注释风格 一些第三方组件库名称太长,每次使用都需要找文档,然后复制粘贴 部分组件库有自己的Snippets插件 ...
- Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南
Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南 因为平时都是使用 VSCode ESLint + Prettier 检测格式化不规范代码,但是随着接手的项目越来越多 ...
- gradle项目中如何支持java与scala混合使用?
之前写过一篇maven项目中java与scala如何混用,今天来看看gradle项目中如何达到同样的效果: 一.目录结构 基本上跟maven一样,tips:这一堆目录结构不用死记,后面会讲如何用gra ...
- 在PHP项目中使用Standford Moss代码查重系统
Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...
- 03_Android项目中读写文本文件的代码
编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...
- 如何在Eclipse中Debug调试Java代码
背景 有的时候你想debug调试Java的源代码,就想试图在Java源代码中设置断点,在Eclipse中常常会出现Unable to insert breakpoint Absent Line Num ...
- JDK中ThreadDump诊断Java代码中的线程死锁问题
多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...
- unity3D项目中如何避免硬代码(C#)
平时做项目,代码中是不允许出现硬代码的,一般我们是怎么处理的呢? 那么硬代码又是什么呢? 我们俗称的硬代码:eg: label.text = "欢迎来到梦幻岛"; 这样我们俗 ...
- [置顶] oracle 数据库表中转换成java代码
--数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '') , 'private '||decode ...
随机推荐
- 2017-2018-1 JaWorld 团队作业--冲刺6
2017-2018-1 JaWorld 团队作业--冲刺6(20162308) 实现 由于我在冲刺部分负责的是类之间的耦合,所以我就介绍一下本次游戏的总体实现. 我们定义了Sprite类,即精灵类,游 ...
- ngular6开发不完全笔记(三)-- 报错指南
router Uncaught Error: Template parse errors: 'router-outlet' is not a known element: If 'router-out ...
- Execution Order for the ApiController
Execution Order for the ApiController Assuming the request goes into the ApiController scope, the op ...
- idea tomcat启动乱码问题
配置idea的安装文件的编码: JetBrains\IntelliJ IDEA 2016.3\bin中配置文件idea.exe.vmoptions.idea64.exe.vmoptions 添加 -D ...
- LA 7278 Game of Cards(SG函数)
https://vjudge.net/problem/UVALive-7278 题意: 两个人玩游戏,现在有n堆牌,轮到自己时,先在牌堆中选一堆牌,先在牌堆中选择拿走0~k张牌(至少得剩下一张),然后 ...
- 51Nod 1557 两个集合(二分)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1557 题意: 小X有n个互不相同的整数: p1,p2,...,pn .他 ...
- bzoj 1799: [Ahoi2009]self 同类分布 数位dp
1799: [Ahoi2009]self 同类分布 Time Limit: 50 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Descripti ...
- python学习笔记(自定义库文件路径)
博主最近在弄接口自动化.主要是基于python自带的unittest框架.包括 Pubilc模块定义所有接口. Main模块根据业务需求重新封装接口便于测试. config文件导入测试业务的固定参数. ...
- express 调优的一个过程和心得,不错的文章
Netflix的软件工程师Yunong Xiao最近在公司的技术博客上写了一篇文章,分析了他所在的团队在将Netflix网站UI转移到Node.js上时遇到的延迟问题.在文章中他描述了找到问题根本原因 ...
- Android------个人项目(歆语气象通)
歆语气象通: 歆语气象伴随你的身边,便捷生活. 包含了以下功能: 1. 天气预报数据覆盖中国城市和地区:2. 提供一周天气预报及最低最高温度,时刻关注天气,轻松计划出行:3. 各种指数详细信息,如太阳 ...