Lombok可以通过注解形式帮助开发人员解决POJO冗长问题,帮助构造简洁和规范的代码,通过注解可产生相应的方法。
我们都知道,使用一种工具,一定要在Maven中添加相应的依赖
在pom.xml中添加依赖
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </dependency>
然而,你会发现除了依赖,你还需要下载IntelliJ Lombok plugin插件
在IDEA中下载插件
Lombok为POJO自动生成getter和setter方法
自动生成toString()方法,默认情况,按顺序(以“,”分隔)打印你的类名称以及每个字段。也可以设置不包含哪些字段@ToString(exclude = {“id”,”name”})
自动重写Equals和hashCode方法
会生成一个含所有参数的构造函数
会生成一个无参构造函数
包含@Getter、@Setter、@ToString、@EqualsAndHashCode和@NoArgsConstructor几项功能
类上注解,直接调用log即可。log.info(****)
自动化关闭流,相当于jdk1.7中的try with resource
@Cleanup InputStream in = new FileInputStream("***"); @Cleanup OutputStream out = new FileOutputStream("***");
public NonNullExample(Student student){
this.student = student;
}
转换成:
public NonNullExample(Student student){ if(student == null){ throw new NullPointException("student"); } this.student = student; }
给方法加上同步锁,建议在代码块中写Synchronized
参考文章 链接:https://www.cnblogs.com/qnight/p/8997493.html
原文:https://www.cnblogs.com/wulachen/p/10691571.html