tctip demo页面>

LomBok?What is it?

01、Lombok 的自我介绍

LomBok官网上如此介绍它:

Project Lombok makes java a spicier language by adding ‘handlers’ that know how to build and compile simple, boilerplate-free, not-quite-java code

据我已有的经验来看,Lombok 最大的好处就在于通过注解的形式来简化 Java 代码,简化到什么程度呢?

我相信你一定写过不少的 getter / setter,尽管可以借助 IDE 来自动生成,可一旦 Javabean 的属性很多,就免不了要产生大量的 getter / setter,这会让代码看起来不够简练,就像老太婆的裹脚布一样,又臭又长。

class Cmower {
	private int age;
	private String name;
	private BigDecimal money;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getMoney() {
		return money;
	}

	public void setMoney(BigDecimal money) {
		this.money = money;
	}
}

Lombok 可以通过注解的方式,在编译的时候自动为 Javabean 的属性生成 getter / setter,不仅如此,还可以生成构造方法、equals、hashCode,以及 toString。注意是在编译的时候哦,源码当中是没有 getter / setter 等等的。


@Getter
@Setter
class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;
}

哎呀,源码看起来苗条多了,对不对?

02、添加 Lombok 的依赖

如果项目使用 Maven 构建的话,添加Lombok 的依赖就变得轻而易举了。

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.6</version>
	<scope>provided</scope>
</dependency>

其中 scope=provided,就说明 Lombok 只在编译阶段生效。也就是说,Lombok 会在编译期静悄悄地将带 Lombok 注解的源码文件正确编译为完整的 class 文件。

温馨提示:只在项目中追加 Lombok 的依赖还不够,还要为 IDE 添加 Lombok 支持,否则 Javabean 的 getter / setter 就无法自动编译,也就不能被调用。

03、为 IDEA添加 Lombok 支持

在插件中搜索LomBok下载即可

反编译后查看源码

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CmowerLombok.java

package com.cmower.java_demo.lombok;

import java.math.BigDecimal;

class CmowerLombok
{

    CmowerLombok()
    {
    }

    public int getAge()
    {
        return age;
    }

    public String getName()
    {
        return name;
    }

    public BigDecimal getMoney()
    {
        return money;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setMoney(BigDecimal money)
    {
        this.money = money;
    }

    private int age;
    private String name;
    private BigDecimal money;
}

嘿嘿,果然 getter / setter 就在里面,这真是一件令人开心的事情

05、常用的 Lombok 注解

1)@Getter / @Setter

@Getter / @Setter 用起来很灵活,比如说像下面这样:

class CmowerLombok {
	@Getter @Setter private int age;
	@Getter private String name;
	@Setter private BigDecimal money;
}

字节码文件反编译后的内容是:

class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;

	public int getAge() {
		return this.age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return this.name;
	}

	public void setMoney(BigDecimal money) {
		this.money = money;
	}
}

2)@ToString

打印日志的好帮手哦。

@ToString
class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;
}

字节码文件反编译后的内容是:

class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;

	public String toString() {
		return "CmowerLombok(age=" + this.age + ", name=" + this.name + ", money=" + this.money + ")";
	}
}

3)@Data

@Data 注解可以生成 getter / setter、equals、hashCode,以及 toString,是个总和的选项。

@Data
class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;
}

字节码文件反编译后的内容是:

class CmowerLombok {
	private int age;
	private String name;
	private BigDecimal money;

	public int getAge() {
		return this.age;
	}

	public String getName() {
		return this.name;
	}

	public BigDecimal getMoney() {
		return this.money;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setMoney(BigDecimal money) {
		this.money = money;
	}

	public boolean equals(Object o) {
		if (o == this) {
			return true;
		} else if (!(o instanceof CmowerLombok)) {
			return false;
		} else {
			CmowerLombok other = (CmowerLombok) o;
			if (!other.canEqual(this)) {
				return false;
			} else if (this.getAge() != other.getAge()) {
				return false;
			} else {
				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$money = this.getMoney();
				Object other$money = other.getMoney();
				if (this$money == null) {
					if (other$money != null) {
						return false;
					}
				} else if (!this$money.equals(other$money)) {
					return false;
				}

				return true;
			}
		}
	}

	protected boolean canEqual(Object other) {
		return other instanceof CmowerLombok;
	}

	public int hashCode() {
		int PRIME = true;
		int result = 1;
		int result = result * 59 + this.getAge();
		Object $name = this.getName();
		result = result * 59 + ($name == null ? 43 : $name.hashCode());
		Object $money = this.getMoney();
		result = result * 59 + ($money == null ? 43 : $money.hashCode());
		return result;
	}

	public String toString() {
		return "CmowerLombok(age=" + this.getAge() + ", name=" + this.getName() + ", money=" + this.getMoney() + ")";
	}
}

其他的LomBok命令有待你解锁

posted @ 2019-10-22 14:27  张怼怼吖  阅读(241)  评论(0编辑  收藏  举报