Lombok超酷注解大曝光

前言

Lombok有很多的注解,可以帮助我们简化模版代码,比如@Getter@Setter@Data,还有很多的注解可以帮助我们,常用的还有@AllArgsConstructor@NoArgsConstructor

@ToString

ToString可以帮助我们打印POJO类对象的信息,格式化成为一个相对比较好看的样式,方便我们在一些调试或者打印日志信息之中使用。

com.zgy.learn.beautifulcleancode.pojo.User@483d953
People(id=null, name=null, salary=null, birthday=null, telephone=null, email=null, blogPage=null, address=null)    

@Builder

这个和下面的@Accessors注解有点类似,可以开启构造者模式,直接用类名.builder(),就可以给需要的字段设置值了。最后需要使用build()组装成我们需要的对象。

@Builder
public class User {
    private Integer id;
    private String name;
    private Integer salary;
    private String telephone;
    private String email;
    private String address;
}

User buildUser02 = User.builder().id(1).address("深圳").email("helloworld@kkk.com").salary(50000).build();

@Accessors

@Accessors是一个开启链式编程的注解,可以让我们不必每次创建对象,都一个属性一个属性地注入,与@Builder类似,但是更加方便,在POJO类上面,使用@Accessors(chain = true)开启。

@Accessors(chain = true)
public class People {
    private Integer id;
    private String name;
    private Integer salary;
    private Date birthday;
    private String telephone;
    private String email;
    private String blogPage;
    private String address;
}

People people3 = new People().setId(1).setName("法外狂徒李四").setTelephone("110");

@SneakyThrows

Lombok超酷注解大曝光

SneakyThrow

@SneakyThrows把我们必须要处理的受检异常,转换成了RuntimeException或者我们自定义的异常,在编译之后,就会生成对应的仍然是try-catch形式的异常处理的代码,而简化了我们本身的代码。

    @SneakyThrows
    public static void main(String[] args) {
        SpringApplication.run(BeautifulCleanCodeApplication.class, args);

        People people1 = new People(1"张三"50000);
        People people2 = new People(null"法外狂徒"500000);
        People people3 = new People().setId(1).setName("法外狂徒李四").setTelephone("110");
        Preconditions.checkArgument(people1.getSalary() > 60000"你不是50w!");
        check(people2);
        check(people3);

    }

    // 抛出异常, 使用@SneakyThrows, 不去显式处理异常
    public static void check(People people) throws Exception {
        if (people.getSalary() < 500000) {
            throw new Exception("行走的50w");
        }
    }

@RequiredArgsConstructor

@AllArgsConstructor@NoArgsConstructor这两个注解我们也比较常用,前者生成所有字段的构造器,后者生成无参构造器,那么有没有一种可以生成部分参数的构造器呢?有!就是@RequiredArgsConstructor,不过对于字段有如下的条件。

* Generates a constructor with required arguments.
* Required arguments are final fields and fields with constraints such as {@code @NonNull}.

也就是说,字段要是非空的,这个@NonNull也是一个lombok的注解,使用在字段,方法,参数,局部变量等都可以如果是null,则会抛出NullPointerException。

/**
 * If put on a parameter, lombok will insert a null-check at the start of the method / constructor's body, throwing a
 * {@code NullPointerException} with the parameter's name as message. If put on a field, any generated method assigning
 * a value to this field will also produce these null-checks.
 */

可以使用final修饰字段,不过最好还是用@NonNull,如果你的字段可以接受为空的,那最好加上一个空的构造器,添加的只有部分参数的构造器,字段用 @NonNull修饰,就可以既满足不为空的情况,有满足为空的情况。

@NoArgsConstructor
@RequiredArgsConstructor 
public class People {
    @NonNull
    private Integer id;
    @NonNull
    private String name;
    @NonNull
    private Integer salary;
    private Date birthday;
    private String telephone;
    private String email;
    private String blogPage;
    private String address;
}

public static void check() throws Exception {
    People people = new People();
    System.out.println(people);
    People people1 = new People(1"张三"50000);

    if (people1.getSalary() < 500000) {
        throw new Exception("行走的50w");
    }
}

@Data

@Data注解是一个集大成者,一个组合注解,在生成构造器的时候,和@RequiredArgsConstructor注解的使用方式一致。

Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode

我们在添加了@Data注解之后,就可以不用再去添加@Getter @Setter @RequiredArgsConstructor @ToString注解了。

本篇文章来源于微信公众号: 疾风小虎牙

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/13852.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!