使用 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 ...
随机推荐
- 《JDK 8.0 学习笔记》1~3章
第一章 Java平台概论 了解Java的发展历程和相关术语如JDK.JVM.JRE等 第二章 从JDK到IDE 书本介绍了新建Java程序的注意事项以及在cmd和Eclipse环境下如何运行Java, ...
- 20145325张梓靖 《Java程序设计》第8周学习总结
20145325张梓靖 <Java程序设计>第8周学习总结 教材学习内容总结 Logger java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logger类, ...
- 【javascript】数据结构-集合
<!DOCTYPE html> <html> <head> <title>集合</title> <meta charset=" ...
- CentOS日常维护及常用脚本
[root@-.x.x xiewenming]# curl myip.ipip.net 当前 IP:42.62.x.x 来自于:中国 北京 北京 联通/电信 www.17ce.com cdn解析网站 ...
- What's the difference between SDK and Runtime in .NET Core?
What's the difference between SDK and Runtime in .NET Core? Answer1 According to the .Net Core Guide ...
- Cube Solution 2
- 发起图片请求的几种可能性(webkit内核)
网页测试源代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...
- C++中ceil、floor和round的区别
Math类中提供了三个与取整有关的方法:ceil,floor,round,这些方法的作用于它们的英文名称的含义相对应 1.ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3 ...
- 计算机网络七层协议模型 “开放系统互联参考模型”,即著名的OSI/RM模型(Open System Interconnection/Reference Model)
计算机网络七层协议模型 作者:Ryan 时间:2013年10月7日 一.物理层(Physical Layer) OSI模型的最低层或第一层,规定了激活.维持.关闭通信端点之间的机械特性.电气特性 ...
- Linux 常用环境搭建
已有环境 python 2.6.6 jdk 1.7 —tomcat— —jenkins— —jq— —Python 2.7— —pip— —PIL— —Android SDK— —yum or apt ...