Lombok可以减少很多重复代码的书写。比如说getter/setter/toString等方法的编写。
- IDEA安装插件:在IDEA中双击Shift,搜索plugins, 点击之后进入插件管理界面,点击下方的Browser Repositories, 搜索lombok ,点击安装, 安装完成重启IDEA, 安装成功
- 添加依赖
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
# 增加注解后再按快捷键 Ctrl + F12,可以查找到set,get,toString 方法。
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class User {
private Integer id;
private String name;
private String password;
public static void main(String[] args) {
User u = new User();
u.setName("abc");
System.out.println(u.toString());
}
}
- log注解[下面的注解会生成下方等效的java代码]
//@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
//@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
//@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
//@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
//@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
//@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
//@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
- @Data,该注解使用在类上,该注解会提供getter、setter、equals、canEqual、hashCode、toString方法。
- @AllArgsConstructor,该注解使用在类上,该注解提供一个全参数的构造方法,默认不提供无参构造。
- @NoArgsConstructor,该注解使用在类上,该注解提供一个无参构造
- @EqualsAndHashCode 该注解使用在类上,该注解在类级别注释会同时生成equals和hashCode。
- @NonNull 该注解使用在属性上,该注解用于属的非空检查,当放在setter方法的字段上,将生成一个空检查,如果为空,则抛出NullPointerException。
- @Cleanup,该注解使用在属性前,该注解是用来保证分配的资源被释放。在本地变量上使用该注解,任何后续代码都将封装在try/finally中,确保当前作用于中的资源被释放。默认@Cleanup清理的方法为close,可以使用value指定不同的方法名称。
- @ToString 该注解使用在类上,该注解默认生成任何非讲台字段以名称-值的形式输出。
1、如果需要可以通过注释参数includeFieldNames来控制输出中是否包含的属性名称。
2、可以通过exclude参数中包含字段名称,可以从生成的方法中排除特定字段。
3、可以通过callSuper参数控制是否调用父类的toString方法。
- @RequiredArgsConstructor,该注解使用在类上,使用类中所有带有 @NonNull 注解的或者带有 final 修饰的成员变量生成对应的构造方法。
- @Value,这个注解用在 类 上,会生成含所有参数的构造方法,get 方法,此外还提供了equals、hashCode、toString 方法。 注意:没有setter
- @SneakyThrows,该注解使用在方法上,这个注解用在 方法 上,可以将方法中的代码用 try-catch 语句包裹起来,捕获异常并在 catch 中用 Lombok.sneakyThrow(e) 把异常抛出,可以使用 @SneakyThrows(Exception.class) 的形式指定抛出哪种异常。该注解需要谨慎使用。详情参看官方介绍
- @Synchronized,该注解使用在类[类级别的锁]或者实例方法[对象锁]上,Synchronized在一个方法上,使用关键字可能会导致结果和想要的结果不同,因为多线程情况下会出现异常情况。Synchronized 关键字将在this示例方法情况下锁定当前对象,或者class类型的对象上多锁定。这可能会导致死锁现象。一般情况下建议锁定一个专门用于此目的的独立锁,而不是允许公共对象进行锁定。
- @Accessors,主要用于控制生成的getter和setter,主要参数介绍
fluent boolean值,默认为false。此字段主要为控制生成的getter和setter方法前面是否带get/set
chain boolean值,默认false。如果设置为true,setter返回的是此对象,方便链式调用方法
prefix 设置前缀 例如:@Accessors(prefix = "abc") private String abcAge 当生成get/set方法时,会把此前缀去掉
- @Wither 提供了给final字段赋值的一种方法,用该注解的所有字段会构成一个有参的构造函数列表的构造函数,同时每个属性会有with打头的方法,返回当前对象[相当于构造函数]
- @Builder 为你的类生成复杂的构建器API。例如:Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
- @Delegate,该属性的所有对象的实例方法都将被该类代理,例如:
import lombok.Data;
import lombok.experimental.Delegate;
import lombok.extern.java.Log;
import java.util.ArrayList;
@Data
@Log
public class User {
private Integer id;
private String name;
private String password;
@Delegate
private ArrayList<String> collection;
public static void main(String[] args) {
User u = new User();
u.setName("abc");
System.out.println(u.toString());
u.log();
}
public void log(){
log.fine("abc");
}
}
生成的代码[注意toString方法下的所有方法]:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.logging.Logger;
public class User {
private static final Logger log = Logger.getLogger(User.class.getName());
private Integer id;
private String name;
private String password;
private ArrayList<String> collection;
public static void main(String[] args) {
User u = new User();
u.setName("abc");
System.out.println(u.toString());
u.log();
}
public void log() {
log.fine("abc");
}
public User() {
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
public ArrayList<String> getCollection() {
return this.collection;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setCollection(ArrayList<String> collection) {
this.collection = collection;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else {
label59: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label59;
}
} else if (this$id.equals(other$id)) {
break label59;
}
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
Object this$password = this.getPassword();
Object other$password = other.getPassword();
if (this$password == null) {
if (other$password != null) {
return false;
}
} else if (!this$password.equals(other$password)) {
return false;
}
Object this$collection = this.getCollection();
Object other$collection = other.getCollection();
if (this$collection == null) {
if (other$collection != null) {
return false;
}
} else if (!this$collection.equals(other$collection)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $password = this.getPassword();
result = result * 59 + ($password == null ? 43 : $password.hashCode());
Object $collection = this.getCollection();
result = result * 59 + ($collection == null ? 43 : $collection.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ", password=" + this.getPassword() + ", collection=" + this.getCollection() + ")";
}
# 以下全是ArrayList的方法,所有方法都被当前类代理
public void trimToSize() {
this.getCollection().trimToSize();
}
public void ensureCapacity(int arg0) {
this.getCollection().ensureCapacity(arg0);
}
public int size() {
return this.getCollection().size();
}
public boolean isEmpty() {
return this.getCollection().isEmpty();
}
public boolean contains(Object arg0) {
return this.getCollection().contains(arg0);
}
public int indexOf(Object arg0) {
return this.getCollection().indexOf(arg0);
}
public int lastIndexOf(Object arg0) {
return this.getCollection().lastIndexOf(arg0);
}
public Object[] toArray() {
return this.getCollection().toArray();
}
public <T> T[] toArray(T[] arg0) {
return this.getCollection().toArray(arg0);
}
public String get(int arg0) {
return (String)this.getCollection().get(arg0);
}
public String set(int arg0, String arg1) {
return (String)this.getCollection().set(arg0, arg1);
}
public boolean add(String arg0) {
return this.getCollection().add(arg0);
}
public void add(int arg0, String arg1) {
this.getCollection().add(arg0, arg1);
}
public String remove(int arg0) {
return (String)this.getCollection().remove(arg0);
}
public boolean remove(Object arg0) {
return this.getCollection().remove(arg0);
}
public void clear() {
this.getCollection().clear();
}
public boolean addAll(Collection<? extends String> arg0) {
return this.getCollection().addAll(arg0);
}
public boolean addAll(int arg0, Collection<? extends String> arg1) {
return this.getCollection().addAll(arg0, arg1);
}
public boolean removeAll(Collection<?> arg0) {
return this.getCollection().removeAll(arg0);
}
public boolean retainAll(Collection<?> arg0) {
return this.getCollection().retainAll(arg0);
}
public ListIterator<String> listIterator(int arg0) {
return this.getCollection().listIterator(arg0);
}
public ListIterator<String> listIterator() {
return this.getCollection().listIterator();
}
public Iterator<String> iterator() {
return this.getCollection().iterator();
}
public List<String> subList(int arg0, int arg1) {
return this.getCollection().subList(arg0, arg1);
}
public void forEach(Consumer<? super String> arg0) {
this.getCollection().forEach(arg0);
}
public Spliterator<String> spliterator() {
return this.getCollection().spliterator();
}
public boolean removeIf(Predicate<? super String> arg0) {
return this.getCollection().removeIf(arg0);
}
public void replaceAll(UnaryOperator<String> arg0) {
this.getCollection().replaceAll(arg0);
}
public void sort(Comparator<? super String> arg0) {
this.getCollection().sort(arg0);
}
}
网友评论