Lombok安装及使用介绍

Lombok浅析

简介:

      lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码,lombok能够达到的效果就是在源码中不需要写一些通用的方法,但是在编译生成的字节码文件中会帮我们生成这些方法。

安装:

  1. 下载lombok插件:https://www.projectlombok.org/download,放在eclipse/STS安装目录下

        

 

 

 

   2.cmd窗口运行命令:java -jar lombok.jar 

  

成功后出现如下界面:

 

3.查看 eclipse/sts.ini,出现如下表示成功,然后重启IDE:

IDEA中的安装

打开IDEA的Setting –> 选择Plugins选项 –> 选择Browse repositories –> 搜索lombok –> 点击安装 –> 安装完成重启IDEA –> 安装成功

使用:

  1. 引入依赖:

在pom.xml中添加如下依赖:

<!-- https://mvnrepository.com/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.org/features/log

@CommonsLog

Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);

@Flogger

Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.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。该注解也会生成一个公共构造函数,可以将任何@NonNullfinal字段作为参数。

虽然@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:

该注解使用在上,该注解在类级别注释会同时生成equalshashCode

@toString

该注解使用在上,该注解默认生成字段以名称-值的形式输出。 

 

@Value

这个注解用在 类 上,会生成含所有参数的构造方法,get 方法,此外还提供了equals、hashCode、toString 方法。 没有setter 

 

 

想了解更多,可以去官方文档查看:http://jnb.ociweb.com/jnb/jnbJan2010.html

  • 9
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值