catalogue
brief introduction
@The main function of the Data annotation is to improve the simplicity of the code. Using this annotation can save a large number of methods in the code, such as get(), set(), toString().
lombok pom dependency
If you want to use @ Data annotation, you should first introduce lombok. What is lombok? It is a tool class library. You can use simple annotation to simplify code and improve development efficiency.
Add dependency in maven
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency>
use
Directly add @ Data annotation to the corresponding entity class; For example, a simple Person class is written in a conventional way:
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 + '\'' + '}'; } }
Write @ Data:
@Data @AllArgsConstructor @NoArgsConstructor public class Person { private String name; private String address; private Integer age; private String hobbit; private String phone; }
Methods of automatic generation:
Common notes:
- @Data: Note: on the class, get, set, equals, hashCode, canEqual and toString methods of the class are provided
- @AllArgsConstructor: note on the class and provide the full parameter construction of the class
- @NoArgsConstructor: note on the class and provide the parameterless construction of the class
- @Setter: Note: on the attribute, set method is provided
- @Getter: Note: on the property, the get method is provided
- @EqualsAndHashCode: Note: on the class, corresponding equals and hashCode methods are provided
- @Log4j/@Slf4j: Note: on the class, the corresponding Logger object is provided, and the variable name is log; Equivalent to
private final static Logger log = LoggerFactory.getLogger(Persion.class);
principle
Lombok is essentially a program that implements the "JSR 269 API". In the process of using javac, the specific process of its effect is as follows:
javac analyzes the source code and generates an abstract syntax tree (AST)
During the operation, the Lombok program called "JSR 269 API" was invoked.
At this time, Lombok processes the ast obtained in the first step, finds the syntax tree (AST) corresponding to the class where the @ Data annotation is located, then modifies the syntax tree (AST) and adds the corresponding tree nodes defined by getter and setter methods
javac uses the modified abstract syntax tree (AST) to generate bytecode files, that is, adding new nodes (code blocks) to class
Advantages and disadvantages
advantage:
- It can automatically generate constructors, getters / setters, equals, hashcode, toString and other methods in the form of annotations, which improves the development efficiency.
- Make the code concise and don't pay too much attention to the corresponding methods.
- When modifying properties, it also simplifies the maintenance of getter/setter methods generated for these properties.
Disadvantages:
- Overloading of multiple parameter constructors is not supported.
- Although it saves the trouble of manually creating getter/setter methods, it greatly reduces the readability and integrity of the source code and reduces the comfort of reading the source code.
- A plug-in like lombok is not just a plug-in. It changes bytecode generation by operating AST (abstract syntax tree) during compiler compilation. In disguise, it is changing java syntax. It changes the way you write source code. Unlike spring's dependency injection, it is a runtime feature, but a compile time feature. If a project has many such plug-ins, it will greatly reduce the comfort of reading source code.
- lombok only saves some trouble of manually generating code, but these getter/setter methods can also be easily generated by using the shortcut keys of IDE. Moreover, sometimes adding a little bit of business code to getter/setter (but it is usually not recommended) can greatly simplify the code of some business scenarios.