Collection of SpringBoot annotations

table of Contents

1 @SpringBootApplication

2 @ImportResource

3.@Autowired

4 Controller related

5.Bean related


1 @SpringBootApplication

Used on the main class of Spring Boot, it indicates that this application is a Spring Boot application. @SpringBootApplication is a combination of three annotations, including:

  • @SpringBootConfiguration: derived from @Configuration, its role is to mark the current class as a configuration class;

  • @EnableAutoConfiguration: Automatic configuration , allowing Spring Boot applications to load all @Configuration configurations into the IoC container;

  • @ComponentScan: Register some beans with specific annotations in the IoC container , such as @Controller, @Component, @Entity, etc.

2 @ImportResource

Used to load the configuration in the xxxx.xml file; for example, when you migrate an old Spring project to Spring Boot, you can use this tag to load the configured configuration file

@SpringBootApplication

@ImportResource("classpath:beans.xml")

public class Chapter1Application { 

  public static void main(String[] args) {         
    SpringApplication.run(Chapter1Application.class, args); }
}

3.@Autowired

Automatic assembly will query the corresponding type of Bean in the IoC container. If the query is found, it will be assembled. If the query is not found, an exception will be thrown; @Autowired can eliminate the set and get methods

4 Controller related

  • @Controller: Marked on a class, it means that this is a Controller object, which is a controller class;

  • @RestController: Equal to the combination of @Controller and @ResponseBody, the object returned by the method can be directly converted into JSON format;

  • @RequestParam: Receive the content in the Request Head. For example, Content-Type identifies the resource type in the specific request, commonly used application/xml, application/json, etc.

  • @PathVariable: Get the parameters in the url;

  • @RequestBody: Read the Request Body and deserialize it into a Java object;

  • @RequestMapping: Map the http request to the controller, that is, bind the request path to its processing classes and methods.

5.Bean related

  • @Service: Used to annotate service layer components, indicating that this is a Bean. The name of the Bean is the name of the current class by default. You can also pass parameters to specify the name of the Bean;

  • @Repository: database access component, which is the DAO layer;

  • @Scope: Used to configure the scope, such as setting singleton to indicate singleton, prototype creates a new object every time, etc.;

  • @Entity: Entity class, you can use the @Table annotation to bind to the table in the database, the primary key is annotated with @Id, and the remaining fields are annotated with @Column (if you keep the attribute name and the field name consistent, you can omit @Column);

  • @Component: When the component is not easy to classify, you can use @Component for annotation;

  • @Autowired: When we need to use components, we can use the automatic injection of Bean implemented by @Autowired.

Guess you like

Origin blog.csdn.net/weixin_40791454/article/details/107040192