Lombok 可以帮助我们减少java中冗长的代码,也算是一种注解式的自动生成工具。
下载安装
自动安装:进入到存放 lombok.jar 的那个目录,并键入 java -jar lombok.jar
。
或
手动安装:
- 将lombok.jar copy到eclipse的安装目录。
- 修改eclipse.ini文件,加入如下 代码
-Xbootclasspath/a:lombok.jar
-javaagent:lombok.jar
原始JavaBean
public class Lure {
private String name;
private int size;
private String color;
private String style;
}
使用Lombok(自动生成getters,setters)
import lombok.Data
public @Data class Lure {
private String name;
private int size;
private String color;
private String style;
}
其他属性还有
-
Never write
public int getFoo() {return foo;}
again. -
No need to start a debugger to see your fields: Just let lombok generate a
toString
for you! -
Equality made easy: Generates
hashCode
andequals
implementations from the fields of your object. -
All together now: A shortcut for
@ToString
,@EqualsAndHashCode
,@Getter
on all fields, and@Setter
on all non-final fields. You even get a free constructor to initialize your final fields! -
Automatic resource management: Call your
close()
methods safely with no hassle. -
synchronized
done right: Don't expose your locks. - To boldly thro
@Getter
/ @Setter
@ToString
@EqualsAndHashCode
@Data
@Cleanup
@Synchronized
@SneakyThrows
推荐文章:http://www.ibm.com/developerworks/cn/opensource/os-lombok/index.html?ca=drs-cn-0411