Lombok浅析
简介:
lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码,lombok能够达到的效果就是在源码中不需要写一些通用的方法,但是在编译生成的字节码文件中会帮我们生成这些方法。
安装:
- 下载lombok插件:https://www.projectlombok/download,放在eclipse/STS安装目录下
2.cmd窗口运行命令:java -jar lombok.jar
成功后出现如下界面:
3.查看 eclipse/sts.ini,出现如下表示成功,然后重启IDE:
IDEA中的安装
打开IDEA的Setting –> 选择Plugins选项 –> 选择Browse repositories –> 搜索lombok –> 点击安装 –> 安装完成重启IDEA –> 安装成功
使用:
- 引入依赖:
在pom.xml中添加如下依赖:
<!-- https://mvnrepository/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
注解解析:
Lombok的使用方法只需在对用类或方法添加对应注解即可
注解种类:
@Setter
@Getter
@Data
@Log(这是一个泛型注解,具体有很多种形式)
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@NonNull
@Cleanup
@ToString
@RequiredArgsConstructor
@Value
@SneakyThrows
@Synchronized
注解详解:
@Log:有如下种类:具体参考官方文档:
https://projectlombok/features/log
@CommonsLog
Creates private static final org.apachemons.logging.Log log = org.apachemons.logging.LogFactory.getLog(LogExample.class);
@Flogger
Creates private static final com.googlemon.flogger.FluentLogger log = com.googlemon.flogger.FluentLogger.forEnclosingClass();
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
默认情况下,记录器的主题(或名称)将是使用注释进行@Log
注释的类的类名称。这可以通过指定topic
参数来定制。例如:@XSlf4j(topic="reporting")
。
@Getter
,
@Setter
:
该注解使用在类或者属性上,该注解可以使用在类上也可以使用在属性上。生成的getter遵循布尔属性的约定。例如:boolean类型的Foo,getter方法为isFoo
而不是getFoo
在使用该注解时,会默认生成一个无参构造。和对应的getter, setter方法 。
以下为官网样例:
@Data
:
该注解使用在类上,该注解是最常用的注解,它结合了@ToString,@EqualsAndHashCode, @Getter和@Setter。本质上使用@Data
注解,类默认@ToString
和@EqualsAndHashCode
以及每个字段都有@Setter
和@getter
。该注解也会生成一个公共构造函数,可以将任何@NonNull
和final
字段作为参数。
虽然@Data
注解非常有用,但是它没有与其他注解相同的控制粒度。@Data
提供了一个可以生成静态工厂的单一参数,将staticConstructor
参数设置为所需要的名称,Lombok自动生成的构造函数设置为私有,并提供公开的给定名称的静态工厂方法。
以下为官网样例:
Lombok annotated code:
@Data(staticConstructor="of")
public class Company {
private final Person founder;
private String name;
private List<Person> employees;
}
Equivalent Java source code:
public class Company {
private final Person founder;
private String name;
private List<Person> employees;
private Company(final Person founder) {
this.founder = founder;
}
public static Company of(final Person founder) {
return new Company(founder);
}
public Person getFounder() {
return founder;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public List<Person> getEmployees() {
return employees;
}
public void setEmployees(final List<Person> employees) {
this.employees = employees;
}
@java.lang.Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != this.getClass()) return false;
final Company other = (Company)o;
if (this.founder == null ? other.founder != null : !this.founder.equals(other.founder)) return false;
if (this.name == null ? other.name != null : !this.name.equals(other.name)) return false;
if (this.employees == null ? other.employees != null : !this.employees.equals(other.employees)) return false;
return true;
}
@java.lang.Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + (this.founder == null ? 0 : this.founder.hashCode());
result = result * PRIME + (this.name == null ? 0 : this.name.hashCode());
result = result * PRIME + (this.employees == null ? 0 : this.employees.hashCode());
return result;
}
@java.lang.Override
public java.lang.String toString() {
return "Company(founder=" + founder + ", name=" + name + ", employees=" + employees + ")";
}
}
@NonNull
:
该注解使用在属性上,该注解用于属的非空检查,当放在setter方法的字段上,将生成一个空检查,如果为空,则抛出NullPointerException
。 该注解会默认是生成一个无参构造。
@EqualsAndHashCode:
该注解使用在类上,该注解在类级别注释会同时生成equals
和hashCode
。
@toString
该注解使用在类上,该注解默认生成字段以名称-值的形式输出。
@Value
这个注解用在 类 上,会生成含所有参数的构造方法,get 方法,此外还提供了equals、hashCode、toString 方法。 没有setter
想了解更多,可以去官方文档查看:http://jnb.ociweb/jnb/jnbJan2010.html
更多推荐
Lombok安装及使用介绍
发布评论