当前位置:网站首页>Farewell to Lombok in 996

Farewell to Lombok in 996

2022-06-25 05:37:00 Which floor do you rate moto

Realization principle

Examples of common annotations

  rely on

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
    // Express jar Packages run at compile time 
    <scope>provided</scope>
</dependency>

Explanation of notes

  • @Getter: Generated field get Method
    • @Getter(lazy = true) : Lazy loading , Must use final Decorate the field value
    • @Getter(value = AccessLevel.PRIVATE,onmethod _={@Notnull})
      • value = AccessLevel.PRIVATE: Set the generated get Method's access level
      • onmethod _={@Notnull}: For the generated get Method to add additional annotations
  • @Setter annotation : Generate set Method
    • @Setter(value = AccessLevel.PRIVATE,onparam _={@Notnull})
      • onparam _={@Notnull}: Add additional annotations to the parameters
  • @ToString annotation
    • @ToString(exclude = {“ Property value ”}) : The attribute value does not generate tostring Method
  • @EqualsAndHashCode annotation : Generate for this class equals Methods and hashcode Method
    • The annotation also has some parameters that can be set : For example, exclude some fields
  • @Data annotation : Large and comprehensive notes
    • contain :@Getter,@Setter,@ToString,@EqualsAndHashCode
  • @NonNull annotation

         

          Generated after adding annotations .class file  

  • @AllArgsConstructor,@NoArgsConstructor,@RequiredArgsConstructor
    • @AllArgsConstructor: Generate a parametric construct
    • @NoArgsConstructor: Generate a nonparametric construct
    • @RequiredArgsConstructor

        

  •  @Cleanup annotation : Marking on the flow to be closed will automatically close the flow , The effect is equivalent to try-with-source

      

Lombok Advantages and disadvantages

  • advantage
    • Automatically generate template code through annotations , Improve development efficiency
    • The code is concise , Focus only on related attributes
    • After adding a new attribute , There is no need to modify the relevant methods
  • shortcoming
    • Reduces the readability and integrity of the source code
    • Make troubleshooting more difficult
    • need IDE Support for related plug-ins
  • @slf4j annotation ,@Builder annotation ,@singular annotation  
    • @Slf4j annotation : Simplify the log import method to work on classes

    • @Builder annotation : Simplify the process of creating objects

    • @singular annotation : coordination @Builder annotation , Simplify set type operations

    • Log system

           

  • @slf4j annotation
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>

// Bridge package 
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.30</version>
    <scope>test</scope>
</dependency>
  • @Builder Notes and @singular annotation  
    • @Builder Completely separate the creation of objects from the use of objects

    • @Builder Add to class : Static attributes cannot be assigned , Initialized final Field cannot be assigned

    • The initialized values of common attribute fields cannot be brought in this way

@Builder
public class BuilderTest{
    // Static attribute 
    private static Stirng staticField;
    //final attribute 
    private final Stirng finalField;
    //final attribute 
    private final Stirng finalField = " Already initialized final attribute ";
    // General attribute 
    private Stirng staticField;
    // Collection class properties 
    @Singular
    private List<String> listFields;

}

// Already initialized final Field cannot be assigned , Static fields cannot be assigned 
// The initialized values of common attribute fields cannot be brought in this way 
// add @Singular after :builder Object time , You can add a field string , You can also add a collection 

 

原网站

版权声明
本文为[Which floor do you rate moto]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210504099583.html