Lombok Introduction & Usage

1) The main reasons for using Lombok

to use Lombok are:

(1) Simplify redundant JavaBean code; (2) greatly improve the execution efficiency of methods in JavaBean;

According to the usage rules of Lombok, if you want to use Lombok, you need to delete the generated getter method setter method and equals and hash methods that exist in the project (JavaBean). Of course, Lombok also provides processing for the print log. This will greatly reduce the amount of code in the project after using Lombok, and because Lombok has the function of automatic modification, it also provides the efficiency of the execution of the code in the project.

2) Install Lombok plugin in IDEASetting -> Select Plugins option -> Select Browse repositories -> Search lombok -> Click Install -> Install to restart IDEA -> Install successfully The following picture is already installed: 这里写图片描述

2)Lombok introduction

  1. Introducing Lombok dependency package
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
 </dependency>
  1. Getter/Setter The annotations here are added to the java class, and can also be added to the specific properties according to the requirements 这里写图片描述 打开我们对应的class文件: 这里写图片描述 可以看出lombok已经帮我们添加了get/set方法,这样我们的程序少了这样繁琐的代码 3)Lombok implementation principleLombok implements the JSR 269 API specification, javac supports this specification in Java6, as long as the program is implemented The API can be called when javac is running, then Lombok uses javac at compile time. The specific process is as follows: 这里写图片描述 首先是项目的源代码文件,在经过编译处理以后,lombok会使用自己的抽象语法树去进行注解的匹配,如果在项目中的某一个类中使用了lombok中的注解,那么注解编译器就会自动去匹配项目中的注解对应到在lombok语法树中的注解文件,并经过自动编译匹配来生成对应类中的getter或者setter方法,达到简化代码的目的。执行顺序就如上图的叠放顺序。

4) Lombok annotations explained Lombok main commonly used annotations are: @Data, @getter, @setter, @NoArgsConstructor, @AllArgsConstructor, @ToString, @EqualsAndHashCode, @Slf4j, @Log4j. Let's look at one by one:

@Data annotation: used in JavaBean or JavaBeans, this annotation has the widest scope, it contains getter, setter, NoArgsConstructor annotations, that is, when using the current annotation, it will automatically generate the inclusion All methods;

@getter annotation: used in JavaBean or class JavaBean, use this annotation will generate the corresponding getter method;

@setter annotation: used in JavaBean or class JavaBean, use this annotation will generate Corresponding setter method;

@NoArgsConstructor annotation: used in JavaBean or JavaBeans, using this annotation will generate the corresponding no-argument constructor;

@AllArgsConstructor annotation: used in JavaBean or JavaBean, use This annotation will generate the corresponding parameter constructor;

@ToString annotation: used in JavaBean or class JavaBean, using this annotation will automatically override the corresponding toStirng method;

@EqualsAndHashCode annotation: in JavaBean or Used in JavaBeans, using this annotation will automatically override the corresponding equals method and hashCode method;

@@lf4j: used in classes that need to print logs, This annotation is used when the slf4j print log framework is used in the project, which simplifies the printing process of the log, just call the info method;

@Log4j: used in the class that needs to print the log, when the project uses log4j This annotation is used when printing the log frame, which simplifies the printing process of the log. Just call the info method.

When using the above annotations to process the parameters, the processing method is as follows (take the @ToString annotation as an example, other annotations are the same) @ToString Annotation):

@ToString(exclude=”column”)

meaning: Exclude the element corresponding to the column column, that is, the column parameter is not included in the generation of the toString method;

@ToString(exclude={“column1”,”column2”})

meaning: excludes the elements corresponding to multiple columns, in English The comma in the state is split, that is, the toString method is generated without multiple column parameters;

@ToString(of=”column”)

meaning: only the toString method that contains the parameters of the element corresponding to the column column, that is, only the column is generated when the toString method is generated. Parameter;;

@ToString(of={“column1”,”column2”})

meaning: only toString method that generates parameters containing elements of multiple column columns, which are separated by commas in English state, ie in life A plurality of column contains only parameters toString method;