lombok常用的注解及使用方法
lombok是⼀种简化源码提⾼编程效率的⼯具,⽤于⽣成常⽤的代码。
如何使用lombok
引⼊依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
安装插件
1、打开IDEA左上角File —> Settings ——>Plugins
常用注解汇总
@val 声明变量时能自行推断变量类型, 并且自带 final 属性,
@Data @ToString、@EqualsAndHashCode、
@Getter、@Setter和 @RequiredArgsConstrutor
@Slf4j 生成 log 对象
@Value 与 @Data 类似, 有以下两点区别:
生成的是全参的构造器;
只有 getter 方法, 没有 setter 方法;
@Getter 所有属性的Getter方法
@Setter 所有属性的Setter方法
@Builder 生成链式构造器的代码
@Cleanup 安全释放或者关闭资源, 最常见的场景就是 IO 中关闭流的操作
@NonNull
getter : 如果获取出来属性为 null, 则抛出 NPE
setter : 如果设置属性时, 传入的值为 null, 则抛出 NPE
@ToString 生成 toString() 方法
@SneakyThrows 将抛出的异常吃掉, 减少一些不必要的 try catch 代码
@NoArgsConstructor 生成一个无参构造方法
@AllArgsConstructor 添加一个包含所有属性的Constructor
@EqualsAndHashCode 生成 equals() 和 hashcode() 方法
@RequiredArgsConstrutor 会生成一个包含常量,和标识了NotNull的变量的构造方法
案例
被注释的部分是lombok会帮助我们自动生成的代码。
@Slf4j
@Slf4j
public class Test {
// private static final Logger log=LoggerFactory.getLogger(Test.class);
public static void main(String[] args) {
log.info("Hello world");
}
}
@Builder
Test test = Test.builder()
.id(id)
.page(page)
.build();
@Data
@Builder
public class Test {
private String id;
private String page;
/**
@java.beans.ConstructorProperties({"id", "page"})
Test(Long id, int page) {
this.id = id;
this.page = page;
}
public static TestBuilder builder() {
return new TestBuilder();
}
public TestBuilder toBuilder() {
return new TestBuilder().id(this.id).page(this.page);
}
public static class TestBuilder {
private Long id;
private int page;
TestBuilder() {}
public TestBuilder id(Long id) {
this.id = id;
return this;
}
public TestBuilder page(int page) {
this.page = page;
return this;
}
public Test build() {
return new Test(id, page);
}
public String toString() {
return "Test.TestBuilder(id=" + this.id + ", page="
+ this.page")";
}
*/
}
@SneakyThrows
@Test(expected = RuntimeException.class)
@SneakyThrows
public void test_throw_exception() {
when(HttpClientUtil.get(anyString()).thenThrow(new RuntimeException());
api.test("nice");
}
@Data
@Data
public class User {
private Long id;
private String username;
private String password;
/**
public User() {}
public Long getId() {return this.id;}
public String getUsername() {return this.username;}
public String getPassword() {return this.password;}
public void setId(Long id) {this.id = id; }
public void setUsername(String username) {this.username = username; }
public void setPassword(String password) {this.password = password; }
public boolean equals(Object o) {
if (o == this) { return true; }
...
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $id = this.getId();
result = result * PRIME + ($id == null ? 43 : $id.hashCode());
final Object $username = this.getUsername();
...
return result;
}
protected boolean canEqual(Object other) {return other instanceof User;}
public String toString() {
return "User(id=" + this.getId() + ...+ ")";
}
*/
}
}
@Value
@Value
public class Test {
(private final) Long id;
(private final) String page;
/**
@java.beans.ConstructorProperties({"id", "page"})
public Test(Long id, String page) {
this.id = id;
this.page = page;
}
public Long getId() {return this.id;}
public String getPage() {return this.page;}
public boolean equals(Object o) {
if (o == this) { return true; }
...
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $id = this.getId();
result = result * PRIME + ($id == null ? 43 : $id.hashCode());
...
return result;
}
public String toString() {
return "Test.TestBuilder(id=" + this.id + ", page="
+ this.page")";
}
*/
}
原文链接:https://blog.csdn.net/xue_xiaofei/article/details/128397765