20200210——springboot lombok

The main role is to improve the simple @Data comment code, use the code annotation lot of get (), set (), toString () methods may be omitted;

The introduction of lombok
To use @Data notes first introduced lombok, lombok what is, it is a tool library, you can use a simple form of annotations to simplify the code and improve development efficiency.

Using
simple a class person, it will include many attributes, name, address, age, etc.

public class Person {
    private String name;
    private String address;
    private Integer age;
    private String hobbit;
    private String phone;

    public Person() {
    }

    public Person(String name, String address, Integer age, String hobbit, String phone) {
        this.name = name;
        this.address = address;
        this.age = age;
        this.hobbit = hobbit;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getHobbit() {
        return hobbit;
    }

    public void setHobbit(String hobbit) {
        this.hobbit = hobbit;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", age=" + age +
                ", hobbit='" + hobbit + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

@Data with wording of:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private String name;
    private String address;
    private Integer age;
    private String hobbit;
    private String phone;
}

Very simple, just enough to write property, will be automatically generated method
Here Insert Picture Description

Notes commonly
@Data: Notes on the class, the class provides get, set, equals, hashCode, canEqual , toString method
@AllArgsConstructor: Notes on the class, the class provide a full configuration parameters
@NoArgsConstructor: Notes on the class, the class to provide no arguments configured
@Setter: Notes on the property, there is provided a method set
@Getter: Notes on the property, there is provided a method get
@EqualsAndHashCode: Notes on the class, to provide a corresponding method of equals and hashCode
@ Log4j / @ Slf4j: class Note on Logger provide the corresponding objects, a variable called log

Principle
on Lombok is essentially a realization of "JSR 269 API" program. In the process of using javac, which have an effect process is as follows:

javac source code analysis, generates an abstract syntax tree (AST)
during operation calls to achieve a "JSR 269 API" of Lombok program
at this time on the right Lombok AST first step to get processed, where to find @Data comment respective tree node class corresponding syntax tree (AST), and then modify the syntax tree (AST), increased defines getter and setter methods
javac abstract syntax tree (AST) generated using the modified bytecode files, that is, to add new class node (block)

Published 735 original articles · won praise 42 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/104242736