lombok常用注解及其使用方法

目录

@Data、@Value

@Setter、@Getter、lombok.config

@Builder

@Singular和@Builder联合使用

@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor

@ToStirng

@NotNull

@Accessors(chain = true):使用链式创建

@Synchronized、@SneakyThrows

@Cleanup: 关闭流、连接点

@Log

@EqualsAndHashCode

@UtilityClass

@ExtensionMethod

@FieldDefaults


以以下类为例:

public class User {
    int id;
    String name;
    List<String> list;
}

@Data、@Value

@Data注解在类上,将类提供的所有属性都添加get、set方法,并添加equals、canEquals、hashCode、toString方法

@Value注解用于修饰类,相当于是@Data的不可变形式,因为字段都被修饰为privatefinal,默认的情况下不会生成settter。还有一点更狠的,默认类本身也是final的,不能被继承。

可以看一下这位老哥的帖子:https://blog.csdn.net/weixin_41540822/article/details/86606535

@Setter、@Getter、lombok.config

给类添加set、get方法

参考帖子:https://blog.csdn.net/weixin_41540822/article/details/86606245

@Builder

使用builder模式创建对象

//创建新的对象
User aaa = User.builder().id(1).name("aaa").build();
//修改原有对象的属性值;要求实体上添加@Builder(toBuilder=true)
aaa = User.toBuilder().id(2).name("bbb).build();

@Singular和@Builder联合使用

可以给集合更加方便的添加多条数据

    @Singular(value = "list")
    List<String> list;
 User aaa = User.builder().id(1).name("aaa").list("aaa").list("djsij").build();

参考帖子地址:https://blog.csdn.net/weixin_41540822/article/details/86606562

@NoArgsConstructor、@AllArgsConstructor、@RequiredArgsConstructor

可以创造一个无参构造器、或者有参构造器、第三个生成final或者@notnull修饰的无参或者有参构造器

也是这位老哥的帖子:https://blog.csdn.net/weixin_41540822/article/details/86606513

@ToStirng

可以添加一个toString方法

@NotNull

不能为空,否则抛出空指针异常

@Accessors(chain = true):使用链式创建

//添加注解
@Data
@Accessors(chain = true)
//使用方法
User aaa = new User().setId(1).setName("aaa");

@Synchronized、@SneakyThrows

@Sychronized 是一个处理线程安全问题的annotation, 他的使用方法和关键字 synchronized比较类似,但是有一些不同点就是,关键字synchronized是锁定当前对象(this指针) , 而@Synchronized则会锁定一个private的常量。如果当前类中没有这个常量,就会自动生成一个

    @Synchronized
    public static void hello(){
        System.out.println("hello");
    }
    @Synchronized
    public int hello2(){
        System.out.println("hello");
        return 1;
    }
    @Synchronized
    public void hello3(){
        System.out.println("hello");
    }

 以下是它生成的方法

private static final Object $LOCK = new Object[0];
private final Object $lock = new Object[0];
private final Object readLock = new Object();
  
public static void hello() {
  synchronized($LOCK) {
    System.out.println("hello");
  }
}
  
public int hello2() {
  synchronized($lock) {
    return 1;
  }
}
  
public void hello3() {
  synchronized(readLock) {
    System.out.println("hello");
  }
}

 @SneakyThrows让你的代码拥有try....catch包裹

@SneakyThrows
public static void throwException() {
    String str  = null;
    String[] split = str.split(",");
    System.out.println(split);
}

 实际上

    public SneakyThrowsTest() {}

    public static void throwException() {
        try {
            String str = null;
            String[] split = ((String)str).split(",");
            System.out.println(split);
        } catch (Throwable var2) {
            throw var2;
        }
    }

@Cleanup: 关闭流、连接点

用于处理写入写出流的异常问题,可以让代码简洁

使用前:

public class Cleanup01 {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[1000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r);
        }
      } finally {
        if (out != null) {
          out.close();
        }
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
}

使用后:

public class Cleanup01 {
  public static void main(String[] args) throws IOException {
    @Cleanup InputStream in = new FileInputStream(args[0]);
    @Cleanup OutputStream out = new FileOutputStream(args[1]);
    byte[] b = new byte[1000];
    while (true) {
      int r = in.read(b);
      if (r == -1) break;
      out.write(b, 0, r);
    }
  }
}

@Log

https://blog.csdn.net/weixin_41540822/article/details/86606632

@EqualsAndHashCode

重写equals和hashcode方法。

@UtilityClass

官方文档是这么说的

创建实用程序类的注释。如果使用注释了一个类,则会@UtilityClass发生以下情况:

它被标记为最终。

如果在其中声明了任何构造函数,则会生成错误。否则,将生成一个私有的无参数构造函数。它抛出一个UnsupportedOperationException

所有方法,内部类和类中的字段均标记为静态。

@ExtensionMethod

设置父类

@FieldDefaults

设置属性的使用范围,如private、public等,也可以设置属性是否被final修饰。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会写爬虫的程序员B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值