cutting edge
What convenience can we use Lombok to bring us? This article will tell you the convenience of Lombok.
effect
Please look at the annotation part of the picture below, and you will see annotations such as Get, SET, DATA, Builder, and ToString. These annotations are functions of Lombok. Lombok helps us realize the corresponding functions in advance through annotations. What do you mean? Woolen cloth? For example, the SET annotation, when we set the @Set annotation on the class or the corresponding field, then Lombok will generate the Set method for the annotated field during compilation.
For example, I add the @Data annotation to the following class
We are opening the compiled code of this class
Lombok automatically generates Get, Set, toString, equals and other methods for us. Is it very convenient? There are some other annotations that bring us great convenience in our daily work.
installation method
The installation path is the same as other plugins. The Mac version is Preferences-》plugins. Enter Lombok in the pop-up dialog box. As shown in the figure below, I have already installed it, so it shows Installed, otherwise it shows the install button. Click it and wait for the download to complete , click OK, prompt to restart, after restart, it can be used normally.
How to use
The Lombok plug-in is a bit different from the plug-ins we talked about before. We need to add the Lombok jar package during use, as shown in the figure below
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> <scope>provided</scope> </dependency>
Data
Adding the @Data annotation to the class will automatically generate no-argument construction methods, get methods, set methods, hashCode methods, equals methods, etc. for us.
Getter
Add this annotation to the class or field to automatically generate the corresponding get method for us
Slf4j
Add this annotation to the class to automatically generate a piece of code similar to the following for us. With the ability to write logs, we only need to output logs through log.info during use.
private static final Logger log = LoggerFactory.getLogger(类.class);
Builder
This annotation can be added to the class, method, and construction method. Its function is similar to that of the iterator in the design pattern. When we create an object, we don’t need to use the new keyword, such as the following piece of code
Bonustype bunusType = Bonustype.builder()
.bunusName("张三")
.payTime(new Date())
.build();
With one line of code, you can create a BunusType object and set parameters for the object, isn't it very convenient? In the next article, we will talk about these annotations in detail in Lombok.
plug-in portal