lombok集成

1. idea集成lombok教程:http://blog.csdn.net/Victor_Cindy1/article/details/72772841

2. eclipse集成lombok教程http://blog.csdn.net/cdyjy_litao/article/details/53759928(具体没试过,可以自行查阅)

3. lombok使用介绍

配置

maven

依赖

     <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <version>1.16.8</version>

</dependency>

编译

     <build>

      <plugins>

          <plugin>

              <groupId>org.projectlombok</groupId>

              <artifactId>lombok-maven-plugin</artifactId>

              <version>1.16.6.1</version>

          </plugin>

      </plugins>

</build>

常用注解:

java bean相关

①.@Setter

功能

 

生成setter方法

源码

    @Setter

        public class LombokDemo {

            private Integer id;

            private String name;

        }

②.@Getter

功能

生成getter方法

源码

@Getter

public class LombokDemo {

  private Integer id;

  private String name;

}

③.@ToString

功能

生成toString方法

源码

@ToString

public class LombokDemo {

  private Integer id;

  private String name;

}

④.@Getter(lazy = true)

功能

懒加载属性

注意:

 

这个与上面@Getter不同,那个是修饰在类上的,也可以修饰在属性上。如果有lazy=true只能修饰在属性,并且还要是private final修饰,限制很大

编码

public class LombokDemo {

  @Getter(lazy = true) private final List<Integer> ids = Arrays.asList(1, 2, 3, 4);

  private String name;

}

⑤.@EqualsAndHashCode

功能

生成equals方法与hashCode方法

源码

@EqualsAndHashCode

public class LombokDemo {

    private Integer id;

    private String name;

}

⑥.@NoAragsConstructor

功能

添加一个无参构造函数

注意

 

这个注解在没有其它有参构造函数的情况下使用意义不大,因为在这种情况下java默认会添加一个无参构造函数

源码

@NoArgsConstructor

public class LombokDemo {

  private Integer id;

  private String name;

}

⑦.@AllArgsConstructor

功能

添加一个所有参数的构造函数

源码

@AllArgsConstructor

public class LombokDemo {

  private Integer id;

  private String name;

}

⑧.@RequiredArgsConstructor

功能

生成一个包含必填参数的构造函数

注意

 

要与@NonNull 搭配使用,该注解修饰的属性就是必填参数

源码

@RequiredArgsConstructor

public class LombokDemo {

  @NonNull private Integer id;

  private String name;

}

⑨.@Date

功能

这是一个综合注解了,等于同时使用

@Getter, @Setter, @ToString, @EqualsAndHashCode,@RequiredArgsConstructor

源码

@Data

public class LombokDemo {

  @NonNull private Integer id;

  private String name;

}

 

⑩.@Value

功能

不可变类的@Date, 他会默认给属性加上final

源码

@Value

public class LombokDemo {

  private Integer id;

  private String name;

}

11.@Accessors

功能

 

这个注解要搭配@Getter与@Setter使用,用来修改默认的setter与getter方法的形式

注意

@Accessors有三个参数可以使用

chain 链式的形式

fluent 流式的形式

prefix 生成指定前缀的属性的getter与setter方法,并且生成的getter与setter方法时会去除前缀

源码 chain = true

@Accessors(chain = true)

@Setter

@Getter

public class LombokDemo {

  private Integer id;

  private String name;

}

 

源码 fluent = true

@Accessors(fluent = true)

@Setter

@Getter

public class LombokDemo {

  private Integer id;

  private String name;

}

源码 prefix = "xxx"

@Accessors(prefix = "xxx")

@Setter

@Getter

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

日志相关

@Log4j

源码

@Log4j

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

@CommonsLog

源码

@CommonsLog

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

@Log

源码

@Log

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

@Log4j2

源码

@Log4j2

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

@Slf4j

源码

@Slf4j

public class LombokDemo {

  private Integer xxxId;

  private String name;

}

设计模式相关

 

@Builder

功能

通过建造者模块来生成bean

源码

@Builder

public class LombokDemo {

  private Integer id;

  private String name;

}

@Delegate

功能

 

@Delegate注释的属性,会把这个属性对象的公有非静态方法合到当前类

注意

 

公共 非静态方法

源码

public class LombokDemo {

  @Delegate

  private Integer id;

  private String name;

}

工具相关

@Cleanup

 

功能

 

关闭流

注意

 

关闭流的方式有点怪异,而且没有在finally里面关闭,如果出现异常的就不会关闭了

源码

 

public class LombokDemo {

  public void test() throws IOException {

      @Cleanup InputStream inputStream = new FileInputStream("xxx.txt");

  }

}

@Synchronized

功能

 

给方法加一个同步块

源码

public class LombokDemo {

  @Synchronized

  public void test() throws IOException {

      System.out.println("test");

  }

}

@SneakyThrows

功能

 

忽略异常

源码

public class LombokDemo {

  @SneakyThrows

  public void test() {

      String s = new String("test".getBytes(), "utf-8");

  }

}

@NonNull

功能

 

设置不能为空的参数

源码

public class LombokDemo {

  public void test(@NonNull String val) {

  }

}

 

@UtilityClass

功能

把普通类转为工具类

源码

  @UtilityClass

  public class LombokDemo {

      private Integer id = 1;

      private String name = "kiwi";

      public void util(){

          System.out.println("xxx");

      }

  }

详情请查看:http://blog.csdn.net/u011719271/article/details/53842420

猜你喜欢

转载自my.oschina.net/1107156537/blog/1647794
今日推荐