@SpringBootApplication

@springBootApplication is a composite annotation that functions as three annotations:

  • @SpringBootConfiguration: is a special @Configuration annotation. The @Configuration annotation is used to declare this class as a Configuration class.
  • @EnableAutoConfiguration: Enables the SpringBoot automatic configuration function to automatically assemble components required by the application.
  • @ComponentScan: Enable component scanning for Spring’s automatic discovery use@Component / @Controller / @ServiceAnnotation annotation class.

@Controller / @Service / @Repository

Except for the name, the function of these annotations is identical. Their main function is to have @ComponentScan recognize this class as a component and create the Bean for that class to add to the container. Stating different annotations helps to identify different types of responsibilities.

Annotations are common in Lombok

@Data

@data tells Lombok to generate all the missing methods at compile time: getter/setter/toString/hashCode/etc.

Using the Idea structure, for example, a class User with three fields:

public class User {
    private int id;
    private String userName;
    private int age;
}
Copy the code

The structure in Idea is:

When you annotate @Data on a class, the structure becomes

Obviously, @data adds:

  • Getters and setters for all fields
  • No parameter constructor
  • equals() / canEqual() / toString() / hashCode()function

@RequiredArgsConstructor

This annotation generates a constructor that takes a field modified by final or annotated by @nonNULL, or a no-argument constructor if no field is qualified.

Using the User class as an example, annotate the domain ID with @nonnull, modify the domain userName with final, and annotate the class with @requiredargsconstructor:

@RequiredArgsConstructor
public class User {
    @NonNull
    private int id;
    final private String userName;
    private int age;
}
Copy the code

The structure of this class in Idea becomes:

@Slf4j

Slf4j stands for Simple Logging Facade for Java. Slf4j creates a log variable for the current class without having to declare the log variable again within the class.

The log variable within the class corresponds to the statement:

Logger log = org.sjf4j.LoggerFactory.getLogger(this.class);
Copy the code

@RequestMapping

RequestMapping is a class-level annotation that marks the class as the one that handles HTTP requests. This annotation can accept a path parameter to specify the processing path, and a method parameter to subdivide the processing of HTTP requests.

@RequestMapping(path = "/home", method = RequestMethod.GET) // Handle HTTP GET requests for the path '/home'
class HomeController {... }Copy the code

In practice, another set of annotations is usually used instead:

annotations describe
@GetMapping Process HTTP GET requests
@PostMapping Process HTTP POST requests
@PutMapping Process HTTP PUT requests
@DeleteMapping Process HTTP Delete requests
@PatchMapping Process HTTP Patch requests

The above five annotations do not apply to classes, only to methods. @xxxMapping(” YYy “) is equivalent to @requestMethod.xxx (path = “yyy”, method = requestMethod.xxx).

The path for method annotation processing follows the path for class annotation processing. For example, if the class annotation is @requestMapping (“/class”) and the method annotation is @getMapping (“/method”), the method handles HTTP GET requests for the path /class/method.