Lombok 是一种 Java插件,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO),它通过注解实现这一目的。
文档:https://projectlombok.org/features/
idea配置:Preferences-》Plugins-》Browse repositories
gradle dependecies配置:compile('org.projectlombok:lombok:1.16.16')
val
Finally! Hassle-free final local variables.
var
Mutably! Hassle-free local variables.
@NonNull
or: How I learned to stop worrying and love the NullPointerException.
@Cleanup
Automatic resource management: Call your close()
methods safely with no hassle.
@Getter/@Setter
Never write public int getFoo() {return foo;}
again.
@ToString
No need to start a debugger to see your fields: Just let lombok generate a toString
for you!
@EqualsAndHashCode
Equality made easy: Generates hashCode
and equals
implementations from the fields of your object..
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
Constructors made to order: Generates constructors that take no arguments, one argument per final / non-nullfield, or one argument for every field.
@Data
All together now: A shortcut for @ToString
, @EqualsAndHashCode
, @Getter
on all fields, and @Setter
on all non-final fields, and @RequiredArgsConstructor
!
@Value
Immutable classes made very easy.
@Builder
... and Bob's your uncle: No-hassle fancy-pants APIs for object creation!
@SneakyThrows
To boldly throw checked exceptions where no one has thrown them before!
@Synchronized
synchronized
done right: Don't expose your locks.
@Getter(lazy=true)
Laziness is a virtue!
@Log
Captain's Log, stardate 24435.7: "What was that line again?"
experimental
Head to the lab: The new stuff we're working on.
注解说明
val
:用在局部变量前面,相当于将变量声明为final@NonNull
:给方法参数增加这个注解会自动在方法内对该参数进行是否为空的校验,如果为空,则抛出NPE(NullPointerException)@Cleanup
:自动管理资源,用在局部变量之前,在当前变量范围内即将执行完毕退出之前会自动清理资源,自动生成try-finally这样的代码来关闭流@Getter/@Setter
:用在属性上,再也不用自己手写setter和getter方法了,还可以指定访问范围@ToString
:用在类上,可以自动覆写toString方法,当然还可以加其他参数,例如@ToString(exclude=”id”)排除id属性,或者@ToString(callSuper=true, includeFieldNames=true)调用父类的toString方法,包含所有属性@EqualsAndHashCode
:用在类上,自动生成equals方法和hashCode方法@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
:用在类上,自动生成无参构造和使用所有参数的构造函数以及把所有@NonNull属性作为参数的构造函数,如果指定staticName = “of”参数,同时还会生成一个返回类对象的静态工厂方法,比使用构造函数方便很多@Data
:注解在类上,相当于同时使用了@ToString
、@EqualsAndHashCode
、@Getter
、@Setter
和@RequiredArgsConstrutor
这些注解,对于POJO类
十分有用@Value
:用在类上,是@Data的不可变形式,相当于为属性添加final声明,只提供getter方法,而不提供setter方法@Builder
:用在类、构造器、方法上,为你提供复杂的builder APIs,让你可以像如下方式一样调用Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
更多说明参考Builder@SneakyThrows
:自动抛受检异常,而无需显式在方法上使用throws语句@Synchronized
:用在方法上,将方法声明为同步的,并自动加锁,而锁对象是一个私有的属性$lock
或$LOCK
,而java中的synchronized关键字锁对象是this,锁在this或者自己的类对象上存在副作用,就是你不能阻止非受控代码去锁this或者类对象,这可能会导致竞争条件或者其它线程错误@Getter(lazy=true)
:可以替代经典的Double Check Lock样板代码@Log
:根据不同的注解生成不同类型的log对象,但是实例名称都是log,有六种可选实现类
@CommonsLog
Creates log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);@Log
Creates log = java.util.logging.Logger.getLogger(LogExample.class.getName());@Log4j
Creates log = org.apache.log4j.Logger.getLogger(LogExample.class);@Log4j2
Creates log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);@Slf4j
Creates log = org.slf4j.LoggerFactory.getLogger(LogExample.class);@XSlf4j
Creates log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);val示例
2 3 4 5 6 7 8 9 10 |
|
@NonNull示例
2 3 4 |
|
2 3 4 5 6 7 8 9 |
|
@Cleanup示例
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
@Getter/@Setter示例
2 3 4 5 |
|
@ToString示例
2 3 4 5 6 7 8 9 10 11 12 |
|
@EqualsAndHashCode示例
2 3 4 5 6 |
|
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor示例
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
@Data示例
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
在IntelliJ中按下Ctrl+F12就可以看到Lombok已经为我们自动生成了一系列的方法。
@Value示例
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
@Builder示例
2 3 4 5 6 7 8 9 10 11 |
|
@SneakyThrows示例
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
@Synchronized示例
2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
@Getter(lazy = true)
2 3 4 5 6 7 8 9 10 11 12 |
|
2 |
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|