Lombok Builder构造器默认值失效

1.16.*及以下版本:@Data默认会生成private的无参构造函数,Builder默认值不生效,但是new的对象默认值生效
1.18.*及以上版本:@Data默认不会生成无参构造函数,Builder默认值生效,但是new的对象默认值不生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test {

public static void main(String[] args) throws Exception {
User user = User.builder().build();
System.out.println("name: " + user.getName());
System.out.println("age: " + user.getAge());
System.out.println("isVip: " + user.isVip());
}

@Data
@Builder
static class User {
private String name = "Joe.Ye";
private int age = 20;
private boolean isVip = true;
}
}

运行结果:

1
2
3
name: null
age: 0
isVip: false

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :