当前位置:网站首页>@Use of data annotation (instead of get and set methods in entity classes)
@Use of data annotation (instead of get and set methods in entity classes)
2022-07-25 08:24:00 【MyBlogHiHi】
Use @Data annotation And lombok, a farewell Java Attribute set and get Method
@Data The main function of annotations is to improve the simplicity of code , Using this annotation can save a lot of... In the code get()、 set()、 toString() Other methods ;
To use @Data Annotations should be introduced first lombok,lombok What is it? , It is a tool class library , Code can be simplified in the form of simple annotations , Improve development efficiency .
stay maven Add dependency to :
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
Add plug-ins to the compiler
Here we use IDEA For example , stay setting Of plugin Search inside lombok plugin, Installing a plug-in .( This plug-in must be installed , Otherwise it doesn't work )
Use
Add... Directly to the corresponding entity class @Data Annotations can be ;
Like a simple one Person class , 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 + '\'' +
'}';
}
}
use @Data Writing :
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private String address;
private Integer age;
private String hobbit;
private String phone;
}
Automatic generation of related methods :
A few common notes :
@Data : Note on class , Provide a class of get、set、equals、hashCode、canEqual、toString Method
@AllArgsConstructor : Note on class , Provide the full parameter construction of the class
@NoArgsConstructor : Note on class , Provides a parameterless construction of a class
@Setter : Note on properties , Provide set Method
@Getter : Note on properties , Provide get Method
@EqualsAndHashCode : Note on class , Provide the corresponding equals and hashCode Method
@Log4j/@Slf4j : Note on class , Provide the corresponding Logger object , A variable called log
principle
Lombok In essence, it is a realization “JSR 269 API” The program . In the use of javac In the process of , The specific process of its effect is as follows :
javac Analyze the source code , An abstract syntax tree is generated (AST)
During the operation, the call is implemented “JSR 269 API” Of Lombok Program
here Lombok Just for the first step AST To deal with , find @Data The syntax tree corresponding to the class where the annotation is located (AST), Then modify the syntax tree (AST), increase getter and setter The corresponding tree node of the method definition
javac Use the modified abstract syntax tree (AST) Generate bytecode file , That is to class Add new nodes ( Code block )
Advantages and disadvantages
advantage :
It can generate constructors automatically in the form of annotations 、getter/setter、equals、hashcode、toString Other methods , Improve the development efficiency
Make the code simple , Don't pay too much attention to the corresponding methods
When the property is modified , It also simplifies the maintenance of the getter/setter Such method
shortcoming :
Overload of multiple parameter constructors is not supported
Although the manual creation is omitted getter/setter The trouble with the method , But it greatly reduces the readability and integrity of the source code , Reduce the comfort of reading source code
image lombok This plug-in , It's not just plug-ins anymore , It is compiled by the compiler through the operation AST( Abstract syntax tree ) Change bytecode generation , In disguise, it is changing java grammar , It changes the way you write source code , It is not like spring Dependency injection is also a runtime feature , It's a compile time feature . If a project has many such plug-ins , It will greatly reduce the comfort of reading source code .
lombok It just saves some trouble of manually generating code , But these getter/setter And so on , use IDE Shortcut keys can also be easily generated . Besides, , Sometimes by giving getter/setter Add a little bit of business code ( But this is not usually recommended ), It can greatly simplify the code of some business scenarios .
Use or not , How to choose between , Naturally, it depends on the needs of the project , Flexible use of .
边栏推荐
- Hotel room management system based on jsp+servlet+mysql
- 【着色器实现Shadow投影效果_Shader效果第八篇】
- JVM specification Oracle official website
- nuscenes数据集3D MOT demo,端到端的目标检测和跟踪,检测跟踪联合框架
- efcore在Saas系统下多租户零脚本分表分库读写分离解决方案
- Raspberry Pie 3 connected to WiFi
- Database query optimization
- @Autowired注解的实现原理
- Machine learning theory and case analysis (Part2) -- Regression
- Redis4.0.14 sentinel automatic failover failed
猜你喜欢

Redis分片集群

Test the mock data method of knowing and knowing

Introduction and installation of mongodb

MVC mode three-tier architecture

"Unable to recognize" yarn "item as cmdlet, function, script file

Eureka forced offline service

CM4 development cross compilation tool chain production

第3章业务功能开发(修改线索,数据回显并修改数据)

RK3399开发板I2C4挂载EEPROM实例

JS typewriter animation JS special effect plug-in autotyperjs
随机推荐
JS typewriter animation JS special effect plug-in autotyperjs
How to obtain the intersection / subtraction / Union of two sets by MySQL
Redis learning
Efcore's solution of multi tenant zero script, table and database read-write separation under SaaS system
SSM+JSP+MYSQL实现的宠物领养收养管理系统源码
Redis best practices
Learn when playing No 5 | human high quality examination, right here →
C # introductory series (30) -- exception handling
The difference between batchnorm and layernorm
@Autowired注解的实现原理
阿里云服务的网络解决方案
Blue and white porcelain used by Charles
Ensembles in RNA counts data in TCGA_ ID to gene_ Method of ID
Literature learning (part101) -- converge clustering
Codeforces Round #809 Editorial(A,B,C)
@Principle of Autowired annotation
Message Oriented Middleware
My creation anniversary
Dirty data and memory leakage of ThreadLocal
serialization and deserialization