1、Lombok是什么东东?
官方介绍Lombok项目是一个Java库,它可以自动嵌入你的编辑器和构建工具中,从而减少你的代码量。永远不要再写另一个getter或equals方法,它带有一个注释的你的类有一个功能全面的生成器,自动化你的日志记录变量等等功能。
简单来说就是使用Lombok,通过注解,让你不再需要编写getter、equals等属性方法,减少样板代码的编写、起到提升代码效率的功能。
2、IDEA如何安装Lombok
IDEA开发工具如果需要正常使用Lombok,就需要安装Lombok插件,这样IDEA就可以正常识别Lombok注解,从而可以正常编译项目。今天给大家介绍一下如何通过IDEA安装IDEA插件。
安装方法:
1、工具栏点击File→Settings设置界面→找到Plugins→找到Lombok插件然后点击install→重启IDEA,安装Lombok插件如下图:
2、点击File-- Settings设置界面,开启 AnnocationProcessors如下图:
开启 AnnocationProcessors目的是让Lombok注解在编译阶段起作用。
3、如何使用Lombok
3.1 添加依赖
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
4 、 Lombok常用用法示例
今天主要给大家罗列一下一些比较常用的lombok用法。
4.1 @Getter/@Setter
public class User { @Getter @Setter private Long id; @Getter(AccessLevel.PROTECTED) private String phone; private String password; } //编译后的代码 public class User { private Long id; private String phone; private String password; public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } protected String getPhone() { return this.phone; } }
说明:@Getter @Setter 注解在类上,表示为类中的所有字段生成Getter&Setter方法。也可以使用@Data来代替@Getter @Setter,但是@Data会引入更多的注解。
4.2 @NonNull
作用:给字段赋值时(调用字段的setter方法时),如果传的参数值为null,则会抛出空异常NullPointerException,生成setter方法时会对参数是否为空进行检查。
@Getter @Setter public class User { private Long id; @NonNull private String phone; } //编译后生成的代码 public class User { private Long id; @NonNull private String phone; public User() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @NonNull public String getPhone() { return this.phone; } public void setPhone(@NonNull String phone) { if(phone == null) { throw new NullPointerException("phone"); } else { this.phone = phone; } } }
4.3. @NoArgsConstructor
作用:生成一个无参构造方法。当类中有final字段没有被初始化时,编译器就会报错,这个时候可用@NoArgsConstructor(force = true),然后为没有初始化的final字段设置默认值 0 / false / null, 这样编译器就不会报错。对于具有约束的字段(例如@NonNull字段),不会生成检查或分配,因此请特别注意,正确初始化这些字段之前,这些约束是无效的。
@NoArgsConstructor(force = true) public class User { private Long id; @NonNull private String phone; private final Integer age; } // 编译后的代码 public class User { private Long id; @NonNull private String phone; private final Integer age = null; public User() { } }
4.4、@Data
作用:@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能。
@Data public class User { private Long id; private String phone; private Integer status; } // 编译后的代码 public class User { private Long id; private String phone; private Integer status; public User() { } public Long getId() { return this.id; } public String getPhone() { return this.phone; } public Integer getStatus() { return this.status; } public void setId(Long id) { this.id = id; } public void setPhone(String phone) { this.phone = phone; } public void setStatus(Integer status) { this.status = status; } 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 { label47: { Long this$id = this.getId(); Long other$id = other.getId(); if(this$id == null) { if(other$id == null) { break label47; } } else if(this$id.equals(other$id)) { break label47; } return false; } String this$phone = this.getPhone(); String other$phone = other.getPhone(); if(this$phone == null) { if(other$phone != null) { return false; } } else if(!this$phone.equals(other$phone)) { return false; } Integer this$status = this.getStatus(); Integer other$status = other.getStatus(); if(this$status == null) { if(other$status != null) { return false; } } else if(!this$status.equals(other$status)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof User; } public int hashCode() { boolean PRIME = true; byte result = 1; Long $id = this.getId(); int result1 = result * 59 + ($id == null?43:$id.hashCode()); String $phone = this.getPhone(); result1 = result1 * 59 + ($phone == null?43:$phone.hashCode()); Integer $status = this.getStatus(); result1 = result1 * 59 + ($status == null?43:$status.hashCode()); return result1; } public String toString() { return "User(id=" + this.getId() + ", phone=" + this.getPhone() + ", status=" + this.getStatus() + ")"; } }
4.5、@Log
作用:生成log对象,用于记录日志,可以通过topic属性来设置getLogger(String name)方法的参数 例如 @Log4j(topic = “com.xxx.entity.User”),默认是类的全限定名,即 类名.class,log支持以下几种主流日志:
- @Log java.util.logging.Logger
- @Log4j org.apache.log4j.Logger
- @Log4j2 org.apache.logging.log4j.Logger
- @Slf4j org.slf4j.Logger
- @XSlf4j org.slf4j.ext.XLogger
- @CommonsLog org.apache.commons.logging.Log
- @JBossLog org.jboss.logging.Logger
@Log private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); @Log4j private static final Logger log = org.apache.log4j.Logger.Logger.getLogger(UserService.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); @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 public class UserService { public void addUser(){ log.info("add user"); } } import java.util.logging.Logger; public class UserService { private static final Logger log = Logger.getLogger(UserService.class.getName()); public UserService() { } }
5、Lombok的优缺点
优点:可以大大减少代码量,使代码看起来非常简洁,大大提高的代码编写的效率。
缺点:
- 需要添加IDEA插件、侵入性很高这个对同事不友好。
- 代码量减少了,本质上是缺失代码的表现。
- 调式起来会比较麻烦,如果想知道某个类的某个属性get方法被哪些类调用非常麻烦。
- 不利于版本升级
- 虽然省去了手动创建getter/setter方法的麻烦,但大大降低了源代码的可读性和完整性,降低了阅读源代码的舒适度
6、总结
Lombok可以帮我们提高写代码的效率,使代码看起来更简洁,它也有不少的缺点不利于后续的运维等等。大家要根据项目的实际情况酌情考虑是否值得使用Lombok。