使用 Lombok 简化项目中无谓的Java代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用 Lombok 简化项目中无谓的Java代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3024字,纯文字阅读大概需要5分钟。
内容图文
![使用 Lombok 简化项目中无谓的Java代码](/upload/InfoBanner/zyjiaocheng/1173/f504ad52a6684603a4b496e1966e8d46.jpg)
在写使用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. 例子
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
自动 生成了 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 = newbyte[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 代码的,减轻了心理负担。
原文:http://www.cnblogs.com/digdeep/p/4633656.html
内容总结
以上是互联网集市为您收集整理的使用 Lombok 简化项目中无谓的Java代码全部内容,希望文章能够帮你解决使用 Lombok 简化项目中无谓的Java代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。