What is Lombok
Lombok is a tool that can help us simplify and eliminate some necessary but bloated Java code through a simple form of annotation. By using the corresponding annotation, the corresponding method can be generated when compiling the source code.
Official address: https://projectlombok.org/ github address: https://github.com/rzwitserloot/lombok
Lombok installation
- Download Lombok (1.16.20)
Add -javaagent:lombok-1.16.20.jar at the end of the eclipse.ini file
Add maven project dependencies
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
Lombok Common Annotations
@val @var
Weakly typed variables are defined; val-annotated variables declare final types, and var-annotated variables are non-final types
@NonNull
Using @NonNull on a parameter of a method or constructor, lombok will generate a null check statement
@Getter @Setter @Accessors
Add Get and Set methods to the class, and use the @Accessors annotation to allow the set method to return to the class itself, thus realizing chain-style programming
@ToString
Add toString method to class
@EqualsAndHashCode
Add equals and hashCode methods to the class
@NoArgsConstructor
Add a no-argument constructor to a class
@RequiredArgsConstructor
A constructor that adds specified parameters to a class
@AllArgsConstructor
Add a constructor with all parameters to the class
@Data
Equivalent to @ToString + @EqualsAndHashCode + @Getter + @Setter + @RequiredArgsConstructor
@Value
Provide @Getter for final variable
@Builder
Provides builder mode
@SneakyThrows
Convert checked exceptions to unchecked exceptions
@Synchronized
类似 Synchronized 关键字,但是可以隐藏同步锁 (会生成一个内部final对象,锁会加这个内部变量,而不是类本身)
@Log @Slf4j @CommonsLog
生成各种log对象