[Java development tool Lombok] common presentation notes

In the past when the object model code, we need to write the get / set as well as a whole bunch of different constructors and so on. Lombok offers a very good plug-in for us.
In most projects, you only need to use the following centralized Annotation enough, if you need to see more options, please refer to: Portal

  1. @Getter
  2. @Setter
  3. @ToString
  4. @RequiredArgsConstructor Final field generated constructor
      /**
       * java class
       */
      @RequiredArgsConstructor
      class UserVO {
          private final Integer id;
          private final String name;
          private int age;
      }
      
      /**
       * 编译后生成的代码
       */
      class UserVO {
          private final Integer id;
          private final String name;
          private int age;
      
          public UserVO(Integer id, String name) {
              this.id = id;
              this.name = name;
          }
    }
  1. @Data Combination comment
  /**
   * @see Getter
   * @see Setter
   * @see RequiredArgsConstructor
   * @see ToString
   * @see EqualsAndHashCode
   * @see lombok.Value
   */
  @Target(ElementType.TYPE)
  @Retention(RetentionPolicy.SOURCE)
  public @interface Data {
      /**
       * ...
       */
      String staticConstructor() default "";
  }
  1. @Builder Change the original assignment mode
  • before use
    UTOOLS1563926459568.png
  • After using (the builder pattern, it has been widely used in Feign source code)
    UTOOLS1563926487313.png
  1. @Slf4j lombok provided, equivalent to
    public static final Logger LOGGER =
        LoggerFactory.getLogger(UserCenterApplication.class);
/**
 * This annotation is valid for classes and enumerations.<br>
 * @see <a href="https://www.slf4j.org/api/org/slf4j/Logger.html">org.slf4j.Logger</a>
 * @see <a href="https://www.slf4j.org/api/org/slf4j/LoggerFactory.html#getLogger(java.lang.Class)">org.slf4j.LoggerFactory#getLogger(java.lang.Class)</a>
 * @see lombok.extern.apachecommons.CommonsLog &#64;CommonsLog
 * @see lombok.extern.java.Log &#64;Log
 * @see lombok.extern.log4j.Log4j &#64;Log4j
 * @see lombok.extern.log4j.Log4j2 &#64;Log4j2
 * @see lombok.extern.slf4j.XSlf4j &#64;XSlf4j
 * @see lombok.extern.jbosslog.JBossLog &#64;JBossLog
 * @see lombok.extern.flogger.Flogger &#64;Flogger
 */
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Slf4j {
    /** @return The category of the constructed Logger. By default, it will use the type where the annotation is placed. */
    String topic() default "";
}

Guess you like

Origin www.cnblogs.com/zhangpan1244/p/11241486.html
Recommended