zoukankan      html  css  js  c++  java
  • [Java 开发利器Lombok] 常用注解演示

    在以往的对象模型编码时,我们需要写一大堆的get/set以及不同的构造函数等。Lombok为我们提供了一个非常好的插件形式。
    在大多数的项目中,只需要使用到以下集中Annotation就足够了,如果需要查看更多的选项,请参考:传送门

    1. @Getter
    2. @Setter
    3. @ToString
    4. @RequiredArgsConstructor 生成final 字段的构造函数
          /**
           * 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 组合注解
      /**
       * @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 改变原有赋值模式
    • 使用前
      UTOOLS1563926459568.png
    • 使用后(建造者模式,在Feign源码中被大量使用)
      UTOOLS1563926487313.png
    1. @Slf4j lombok 提供,等价于
        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 "";
    }
    
  • 相关阅读:
    MySQL中去重字段完全相同的数据
    SVN自动更新-win平台
    EF出错:Unable to convert MySQL date/time value to System.DateTime
    微信不支持Object.assign
    记录一下dotnetcore.1.0.0-VS2015Tools.preview2安装不上的问题
    Ajax表单异步上传(包括文件域)
    .NET web开发之WebApi初试水
    遍历数组一次求得数组的平均数、标准差、方差
    记STM32F030多通道ADC DMA读取乱序问题
    RT-Thread入门和模拟器的配置生成
  • 原文地址:https://www.cnblogs.com/zhangpan1244/p/11241486.html
Copyright © 2011-2022 走看看