面试的时候,有问起这个lombok是什么玩意儿? 当时没有使用过,感觉挺好奇的,简单研究了下
打开下方蓝色注解链接-有惊喜
lombok官方地址: https://projectlombok.org/download.html
使用Maven来添加支持
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
使用idea插件,需要安装,设置下。
下面贴一些官网的demo:
val
- Finally! Hassle-free final 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
andequals
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-null field, 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?"
- Lombok, made to order: Configure lombok features in one place for your entire project or even your workspace.
- Here be dragons: Extra features which aren't quite ready for prime time yet.