使用lombok來簡化你的Java Bean

能作什麼?

在使用lombok以前:java

public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String[] getAuthors() {
        return authors;
    }

    public void setAuthors(String[] authors) {
        this.authors = authors;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Book)) return false;
        if (!super.equals(o)) return false;

        Book book = (Book) o;

        if (getId() != null ? !getId().equals(book.getId()) : book.getId() != null) return false;
        if (getTitle() != null ? !getTitle().equals(book.getTitle()) : book.getTitle() != null) return false;
        if (getReleaseDate() != null ? !getReleaseDate().equals(book.getReleaseDate()) : book.getReleaseDate() != null)
            return false;
        if (getPrice() != null ? !getPrice().equals(book.getPrice()) : book.getPrice() != null) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(getAuthors(), book.getAuthors())) return false;
        return getPublisher() != null ? getPublisher().equals(book.getPublisher()) : book.getPublisher() == null;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (getId() != null ? getId().hashCode() : 0);
        result = 31 * result + (getTitle() != null ? getTitle().hashCode() : 0);
        result = 31 * result + (getReleaseDate() != null ? getReleaseDate().hashCode() : 0);
        result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
        result = 31 * result + Arrays.hashCode(getAuthors());
        result = 31 * result + (getPublisher() != null ? getPublisher().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", releaseDate=" + releaseDate +
                ", price=" + price +
                ", authors=" + Arrays.toString(authors) +
                ", publisher='" + publisher + '\'' +
                '}';
    }
}

在使用以後:eclipse

@Data
public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;
}

而且從功能上,使用後=使用前。
說的簡單點:今後之後不須要寫GetterSettertoStringhashCodecanEqualconstructor這些大部分類似,卻又不得不寫的代碼了。ide

怎麼用?

一共有三個步驟函數

  1. 爲你的開發工具安裝插件。工具

    • IntelliJ IDEA: settings->Plugins->Browse Repositories->搜索Lombok Plugin->install開發工具

    • Eclipse / MyEclipse / Spring Tool Suite / JBoss Developer Studio: 看這裏ui

    • Netbeans: 看這裏this

  2. 下載並導入Jar包,下載地址插件

  3. 在須要的類上標註註解code

目前我使用的版本是1.16.16,有下面幾個經常使用註解:

@Date

生成GetterSettertoStringequalshashCode方法。

可標註:Class

@Value

生成GettertoStringequalshashCode、還有全字段的構造方法。而且會將字段全都添加final

可標註:Class

@Builder

在類內部生成一個名爲類名+Builder的內部類,用於快速構建。

可標註:Class

你能夠這樣使用它:

@Builder
public class Book {

    private Integer id;

    private String title;

    private Date releaseDate;

    private Double price;

    private String[] authors;

    private String publisher;

}
public static void main(String[] args) {
    Book book = Book.builder().id(1).title("書名").build();
}

@Getter/@Setter

生成GetterSetter方法。若是標註在類上則會生成這個類中全部字段的GetterSetter方法。

可標註:Field,Class

@NonNull

爲你的構造函數、Setter添加空值校驗。標註的字段賦值爲空時,會拋出NullPointerException

可標註:Field,Method,Parameter,Local Variable

爲何?

自從Java 6起,Javac就支持了一個名叫JSR 269 Pluggable Annotation Processing API的規範,它的做用就是:若是你實現了這個API,在Javac編譯時就會獲得調用。而lombok正是運用了這一點。

流程是這樣的:

  1. Javac編譯源代碼,並生成語法樹(AST)

  2. Javac尋找實現了JSR 269 .... API的代碼(lombok),並調用。

  3. lombok尋找被標註了註解的類,修改Javac生成的語法樹(AST)。

  4. Javac將語法樹生成爲字節碼(.class)

就到這裏了

它還具有不少好用的功能,你能夠去這裏看看。

相關文章
相關標籤/搜索