lombok插件的使用(简化Java代码)
- 先安装插件

- 导入lombok包。如果使用maven构建项目直接在pom.xml中导入下面依赖即可
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
- 使用:直接在类的上面加注解以@Data为例
import lombok.Data;
@Data
public class People {
private int id;
private String username;
private int age;
}
可以看到类的结构中多了很多东西。就是注解的作用
-
其他注解(官网)
@NonNullor: How I learned to stop worrying and love the NullPointerException.
@CleanupAutomatic resource management: Call your
close()methods safely with no hassle.@Getter/@SetterNever write
public int getFoo() {return foo;}again.@ToStringNo need to start a debugger to see your fields: Just let lombok generate a
toStringfor you!@EqualsAndHashCodeEquality made easy: Generates
hashCodeandequalsimplementations from the fields of your object..@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructorConstructors made to order: Generates constructors that take no arguments, one argument per final / non-nullfield, or one argument for every field.
@DataAll together now: A shortcut for
@ToString,@EqualsAndHashCode,@Getteron all fields, and@Setteron all non-final fields, and@RequiredArgsConstructor!@ValueImmutable classes made very easy.
@Builder... and Bob's your uncle: No-hassle fancy-pants APIs for object creation!
@SneakyThrowsTo boldly throw checked exceptions where no one has thrown them before!
@Synchronizedsynchronizeddone right: Don't expose your locks.@WithImmutable 'setters' - methods that create a clone but with one changed field.
@Getter(lazy=true)Laziness is a virtue!
@LogCaptain's Log, stardate 24435.7: "What was that line again?"
experimentalHead to the lab: The new stuff we're working on.
- 缺点
- 代码可读性降低
- 不支持构造器的重载
- 非结构性减轻开发任务。
浙公网安备 33010602011771号