Spring framework annotations

Notes

annotation explain
@Configuration Added before the declaration of the class, indicating that this class is a configuration class, it will automatically execute the @Bean method in the configuration class, and interpret other annotations on the configuration class
@Bean Add before the method used to create objects in the configuration class, so that the Spring framework automatically calls this method and manages the results returned by this method. The @Bean method must exist in the @Configuration class
@Component Component annotations, added before the declaration of the class, indicating that this class is a component class, is a general annotation, and should be added to a class that is not a controller class, nor a business logic class, nor a data access class
@ComponentScan Component scanning, when loading this annotation, Spring will scan whether there is a component class under the root package of this annotation configuration, if it exists, it will automatically create an object of the component class, if some classes are not components, they will be skipped (ignored)
@Controller Component annotations, added before the declaration of the class, indicating that this class is a component class and should be added to the controller class
@Service Component annotation, added before the declaration of the class, indicates that this class is a component class and should be added on the business logic class
@Repository Component annotation, added before the declaration of the class, indicates that this class is a component class and should be added to the data access class
@Scope Configure the scope of the Spring Bean. When configured @Scope("prototype")as , the Spring Bean will be "non-singleton". If you use the @Bean method to create an object, this annotation is added before the @Bean method. If you use component scanning to create an object, this Annotations are added before the component class
@Lazy Configure whether the singleton Spring Bean is "lazy loaded". When this annotation is added before the @Bean method or before the declaration of the component class, the Spring Bean will be lazy loaded. If you use the @Bean method to create an object , this annotation is added before the @Bean method, if the object is created using component scanning, this annotation is added before the component class
@Autowired Annotation for automatic assembly. When a property needs to be assembled by Spring, add this annotation before the property. In addition, this annotation can be added before the Setter method and construction method. Usually, this is not necessary.
@EnableWebMvc The annotation added on the Spring MVC configuration class. When the response body is in JSON format, this annotation must be added, otherwise it will cause a 406 error. In addition, it does not need to be added manually in the Spring Boot project
@ResponseBody Response body, which can be added before the method that handles the request/exception, it will act on the corresponding method, or added before the class, it will act on all the methods in the class that handle the request/handle the exception
@RestController Equivalent to @Controller + @ResponseBody
@RequestMapping It is usually added on the class to configure the prefix part of the request path, and also use the produces attribute to configure the document type when all the methods that process the request in this controller class respond, for example, it is configured on the class as @RequestMapping(value="user" , produces="application/json; charset=utf-8")
@GetMapping It is a @RequestMapping that limits the request type to GET, usually added to the method that processes the request, and is used to configure the request path mapped by this method
@PostMapping Refer to @GetMapping
@RequestParam Add before the parameters of the method that handles the request, which can be used to: specify the request parameter name, configure whether this request parameter must be submitted, configure the default value of the request parameter
@PathVariable Added before the parameters of the method that processes the request, it is only used when the placeholder is used to match the variable value in the request path. If the name in the placeholder is different from the method parameter name, you can also configure the parameters of this annotation to specify the placeholder the name in the bit
@RequestBody Add before the parameters of the method that processes the request. This annotation is only used when the client uses JSON format to organize the request parameters. If the client’s request parameters are in FormData format (using splicing), this annotation cannot be &added
@ExceptionHandler Before adding the exception handling method, the exception type that needs to be handled can be configured through annotation parameters
@ControllerAdvice Added before the class, the code in this class will take effect when processing any request
@RestControllerAdvice Equivalent to @ControllerAdvice + @ResponseBody
@Mapper Add it to (every) interface that accesses data when using Mybatis, making Mybatis clear that this is an interface for accessing data, so Mybatis will generate a proxy object for this interface to achieve the final data access function, but this method is not recommended , see also @MapperScan annotation
@MapperScan Added on the configuration class, it is used to scan the root package where the interface for accessing data when using Mybatis is located. After using this annotation, each interface no longer needs to use the @Mapper annotation. When configuring the root package, you should ensure that there is only access under this package The data interface must not have other interfaces, because Mybatis will not parse the internal code of the interface, as long as it is an interface, it will create a proxy object. If you create a class to implement this interface and the object is created by Spring, an error will occur
@Param Added to the parameters of the abstract method in the interface that accesses data when using Mybatis, it is used to specify the parameter name. When the placeholder is used when configuring the SQL statement, the name in the placeholder is the parameter value of this annotation. When the abstract method When there are more than 1 parameter, you must use this annotation to configure the parameter name
@SpringBootApplication The annotation added to the startup class in the Spring Boot project, each Spring Boot can only have one class to add this annotation, the meta-annotation of this annotation includes @SpringBootConfiguration and @ComponentScan, so the startup class itself is also a configuration class, and the default component scan
@SpringBootConfiguration It is one of the meta-annotations of @SpringBootApplication. The meta-annotations of this annotation include @Configuration, making the Spring Boot startup class a configuration class
@SpringBootTest Add annotations on the test class in the Spring Boot project, so that the test class will load all the configuration and environment in the current project, for example, you can use the automatic assembly mechanism in the test class, use MockMvc (need to add spring-boot-starter-web dependency) Etc. If some test classes are clear and do not need to load the configuration and environment of the current project, this annotation can not be added
@AutoConfigureMockMvc 添加在Spring Boot项目中测试类上的注解,需添加spring-boot-starter-web依赖,当需要对控制器层使用MockMvc进行模拟测试时需要添加此注解,才可以在测试类中自动装配MockMvc对象
@Sql 可以添加在测试类上,也可以添加在测试类的测试方法,如果同时添加,以测试方法上的注解为准;可以在注解中配置参数,使得测试方法执行之前去执行指定的若干个SQL脚本,还可以配置脚本的执行阶段是在测试方法之前或测试方法之后
@Validated 添加在控制器类中处理请求的方法的参数之前,表示此参数将被Validation框架进行验证,具体的验证规则由其它的验证注解来决定
@Valid 在开发实践中,在绝大部分情况下与@Validated相同
@NotNull 验证注解,通常添加在被注解为@Validated/@Valid的类的属性上,表示不允许为null,可以配置message属性,用于指定验证失败时的提示文本
@Range 验证注解,通常添加在被注解为@Validated/@Valid的类的数值型属性上,可通过此注解的min和max属性配置数值型属性的取值区间,可以配置message属性,用于指定验证失败时的提示文本
@Pattern 验证注解,通常添加在被注解为@Validated/@Valid的类的字符串属性上,可通过此注解的regexp属性来指定正则表达式,将应用正则表达式对请求参数进行验证,可以配置message属性,用于指定验证失败时的提示文本
@JsonInclude 可以添加在POJO类上,也可以添加在POJO类的属性上,用于配置响应JSON结果时,什么样的属性会包含在响应结果中,默认值是JsonInclude.Include.ALWAYS,即始终包含,通常建议配置为JsonInclude.Include.NON_NULL,表示“值不为null时被包含在响应结果中”,此配置可以通过在配置文件中配置spring.jackson.default-property-inclusion属性实现全局的配置
@Data 添加在POJO类上的属性,使得Lombok会在编译期自动生成全属性的Settter & Getter、hashCode()、equals()、toString()方法
@Slf4j 添加在任意类上,在类中可以使用名为log变量输出日志

Guess you like

Origin blog.csdn.net/gezongbo/article/details/126264066