Six, Spring Boot container Lombok plug-in detailed use, simplify the configuration, improve development efficiency
@
- Six, Spring Boot container Lombok plug-in detailed use, simplify the configuration, improve development efficiency
- 1. Introduction to Lombok
-
2. Lombok common annotations
- 2.1 @ToString
- 2.2 @Setter
- 2.3 @Data
- 2.4 @AllArgsConstructor
- 2.5 @NoArgsConstructor
- 3. install the lombok plugin in idea
- 4. Summary:
- 6. Finally:
1. Introduction to Lombok
Lombok Role:
- Simplify Java bean development , you can use Lombok's annotations to make the code more concise .
- Java project, a lot of non-technical but must exist in the code ; for example : Pojo getter/setter/toString ; exception handling : I / O stream shutdown operation and so on.
- Java project, a lot of non-technical but must exist in the code: for example: these codes are not technical, but also affect the beauty of the code, Lombok was developed.
Spring Boot and IDEA Official Support
- IDEA 2020 has the Lombok plugin built in.
- Spring Boot versions after 2.0 also have Lombok dependencies built into Stater.
2. Lombok common annotations
@Data: annotated on a class, provides getting and setting methods for all attributes of the class, in addition to equals, canEqual, hashCode, toString methods.
@Setter: annotated on properties to provide setting methods for properties, and on classes to provide set() methods for all properties in the class.
@Getter: annotate on a property to provide the getting method for the property and on a class to provide the get() method for all properties of the class.
@Log4j: annotated on class: provides a log4j log object for the class with property name: log
@NoArgsConstructor: annotates the class to provide a constructor method with no parameters.
@AllArgsConstructor: Annotate the class to provide an all-args constructor.
@Cleanup: allows you to close the stream.
@Builder: Annotate the class with a constructor pattern.
@Synchronized: add a synchronized lock
@SneakyThrows: Equivalent to try/catcher catching exceptions.
@NonNull: annotated with @NonNull: throws a null pointer exception if the parameter is null.
@Value: This annotation is similar to @Value, except that it defines all member variables as private final by default and does not produce set() methods.
Below we test, using a few, more commonly used in developmentexplanatory note 。
First of all, to use Lombok, you need to import the associatedjar
dependencies, you can also use Spring Boot's own built-in ones without importing them.
<! -- Introducing lombok, using version arbitration -- >
<dependency>
<groupId> </groupId>
<artifactId>lombok</artifactId>.
</dependency>.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>springboot_lombok</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- import (data)SpringBoot parent project-style of writing (grammar)-->
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
<dependencies>
<!-- import (data)webProject Scene Launcher:会自动import (data)和webdevelopment-relatedjarPackage all dependencies【storehouse/jar】-->
<!-- I'll explain later.spring-boot-starter-web What exactly are the relevant dependencies introduced-->
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- pull into lombok ,Using version arbitration-->
<dependency>
<groupId></groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2.1 @ToString
@ToString: // At compile time, generate toString, note: by default, a referenceless constructor is generated.
package ;
import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;
@ToString // Generate toString at compile time, note: by default, a constructor is generated with no parameters
public class Furn {
private Double price = 999.0;
}
The lombok annotation can be used to simplify the code by using idea's decompiler, target, to look at the source code.
You can see the complete code generated.
If no target directory is shown in the catalog, you can do the following:
2.2 @Setter
The @Setter annotation: The @Setter annotation provides a setting method for attributes, while the @Setter annotation provides a set() method for all attributes of a class.
First, let's annotate the use of attributes.
package ;
import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;
public class Furn {
@Setter // Annotate the property to compile a set() method for the property: by default, a referenceless constructor is generated.
private Integer id = 100;
private Double price = 999.0;
}
Adding to a class adds the set() method to all properties of the class.
2.3 @Data
@Data : annotation on class, provides getting and setting methods for all attributes of the class, in addition to equals, canEqual, hashCode, toString methods, @RequiredArgsConstructor
Special Note: @RequiredArgsConstructor in @Data
When we write a controller or service layer, we need to inject a lot of mapper interfaces or another service interface.
The code looks messy with the @Autowired annotation. lombok provides an annotation:: @Autowired
@RequiredArgsConstructor(onConstructor=@_(@AutoWired))
Write on the class instead of @Autowired annotation, note that when injecting, you need to define it with final, or use @notnull annotation
package ;
import ;
import ;
import ;
import ;
import ;
import ;
@Data// annotation equivalently uses the,the following note: @Getter,@Setter,@RequiredArgsConstructor @ToString,@EqualsAndHas
public class Furn {
private Integer id = 100;
private String name = "John Doe";
private Double price = 999.0;
}
2.4 @AllArgsConstructor
@AllArgsConstructor: at compile time, an all-args constructor is generated
package ;
import ;
import ;
import ;
import ;
import ;
import ;
@AllArgsConstructor // at compile time,A full-parameter constructor is generated
public class Furn {
private Integer id = 100;
private String name = "John Doe";
private Double price = 999.0;
}
Note: Here: we notice that there is a full-argument constructor, but a default "no-argument constructor" is not generated.
2.5 @NoArgsConstructor
@NoArgsConstructor: At compile time, the no-args constructor is generated (it must be) and is not affected by other
package ;
import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;
@NoArgsConstructor // At compile time, the no-args constructor is generated (it must be) and is not affected by other
public class Furn {
private Integer id = 100;
private String name = "John Doe";
private Double price = 999.0; private String name = "Zhang San"; private Double price = 999.0; private Double price = 999.0
}
Special Notes:
In particular, although the above @Data,@ToString annotations and so on will generate a no-args constructor by default, when you use more than one annotation, the no-args constructor may be overridden. But when we have other constructors generated, you need to use the @NoArgsConstructor annotation if you want to still have a parameterless constructor, because @NoArgsConstructor will definitely generate a parameterless constructor (parameterless constructors are important because the use of the framework involves the reflector mechanism, and the reflector mechanism, which requires a constructor, otherwise you won't be able to do reflection to get the bean object and the framework won't be able to use it).
Here is the test. We add the @Data annotation and the @AllArgsConstructor annotation. It is clear to see that theThe default parameterless constructor is overridden by the
So we need to add on: @NoArgsConstructor annotation, because @NoArgsConstructor annotation, at compile time, generates the uninformed constructor (which will definitely be generated) and will not be overridden by any other influence. As shown in the figure
3. install the lombok plugin in idea
It's possible to use basic annotations like @Data,@Getter without a plugin...
But you can't use its extended features, such as logging output... , so let's just install it, it's also easier.
Go directly to the plugin store and search for: Lombok.
package ;
import ;
import .slf4j.Slf4j;
import ;
import ;
import ;
@Slf4j
@RestController // @Controller + @ResponseBody
public class HiController {
@Autowired
private Furn furn;
@RequestMapping("/hi") // Setting the request mapping path
public String hi(){
("furn-"+furn);
// Placeholder output
("furn={} myfurn={}",furn,furn);
return "hi word";
// utilization slf4jlog output
}
}
Run the test:
4. Summary:
- Familiar with common Lombok annotations to provide development efficiency.
- Basically @ annotations generate a default referenceless constructor, but when there are multiple annotations, this default referenceless constructor, may be overridden, resulting in the inability to not be generated. However, the framework's use of the @ annotation essentially requires the use of the @reflex (i.e. automatic reaction of organism) mechanism, and the reflection mechanism isMust have a parameterless constructor to be able to reflect and get the corresponding bean object. The bean object will be used by the framework. So, a bean object is usually annotated with @NoArgsConstructor, which generates a parameterless constructor at compile time (which it will), and is not affected by other
6. Finally:
"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."