Article directory
- @SpringBootApplication
- @==Component==、@Service、@Controller、@Repository
- @ResponseBody
- @RestController
- @AutoWired、@Qualifier、@Resource
- @RequestMapping、@GetMapping、@PostMapping
- @Value、@ConfigurationProperties、@PropertySource
- @Configuration、@Bean
- @RequestParam、@RequestBody、@PathVariable、@RequestHeader、@CookieValue
Summary of SpringBoot’s common annotations
This article is only for reference from a big boss on csdn to summarize and learn. If there is any infringement, please contact and delete it.
@SpringBootApplication
Including @Configuration, @EnableAutoConfiguration, @ComponentScan are usually used on the main class;
related notes | illustrate |
---|---|
@ComponentScan | Used to automatically scan the classes identified by these annotations, and finally generate beans in the ioc container. The default scanning range is the configuration class package and sub-package classes where the @ComponentScan annotation is located |
@SpringBootConfiguration | It has the same function as @Configuration. It is used to declare that the current class is a configuration class. Here it is the configuration class used by the springboot main class. |
@EnableAutoConfiguration | It is the core annotation for springboot to realize automatic configuration. Through this annotation, the beans required by spring application are injected into the container. |
@Component、@Service、@Controller、@Repository
These annotations are combined because the functions are basically the same. They are all injecting classes into the spring container, but they use different scenarios. The classes annotated by @Component, @Service, @Controller, @Repository annotations, these Classes will be incorporated into the spring container management.
annotation | illustrate |
---|---|
@Repository | The persistence layer (dao) is injected into the spring container |
@Service | The business logic layer (server) is injected into the spring container |
@Controller | The control layer (controller) is injected into the spring container |
@Component | Ordinary pojo injected into spring container |
@ResponseBody
@ResponseBody can act on a method or a class, indicating that the return result of the method is directly written into the HTTP response body, and will not be parsed as a jump path, that is, it will not go through the view parser, and the returned data will be entered on the page what data.
annotation | illustrate |
---|---|
@ResponseBody | The function of @ResponseBody is actually to convert java objects into data in json format. |
@RestController
This annotation is a combination of @Controller and @ResponseBody, which is generally used for classes, and its function is equivalent to adding @ResponseBody and @Controller on the class
@AutoWired、@Qualifier、@Resource
These three annotations are automatically assembled based on the annotation method, and the found beans are returned in the container. Generally, @AutoWired is used the most, @Qualifier needs to be used with @AutoWired, and @Resource can be automatically assembled by name
annotation | illustrate |
---|---|
@AutoWired | @Autowired is assembled by type by default. If multiple beans are found, they will be compared by name. If there are more than one, an exception will be reported. |
@Qualifier | Spring annotations, injection by name Generally when there are two or more beans, I don’t know which one to inject, use it in combination with @AutoWired |
@Resource | By default, inject by name, such as @Resource(name = "zhaozhao"), if the injection cannot be found according to the name attribute, an error will be reported. If there is no name attribute, it will be injected according to the attribute name. If the match fails, it will be matched according to the type. If the match fails, an error will be reported. |
@AutoWired
@Qualifier
When there are multiple implementation classes of an interface, only using @AutoWired will report an error, because it has multiple interface implementation classes. I don’t know which one you are looking for. At this time, you need to start when injecting beans A name, and then use the @Qualifier annotation to specify which bean (injection and assembly according to the name)
@Resource
The use of this annotation is equivalent to the effect of using @AutoWired and @Qualifier together
@RequestMapping、@GetMapping、@PostMapping
The functions of these three annotations are also similar. These three annotations are used to map requests, that is, to specify which URL requests the controller can handle. When used in methods, they can be accessed through the configured url
annotation | illustrate |
---|---|
@RequestMapping | @RequestMapping(url), through this annotation, you can access through the configured url, the method can be get or post request, both methods are available |
@GetMapping | @GetMapping(url), the function is similar, but this limit can only be Get request |
@@PostMapping | @PostMapping(url), the function is similar, but this limit can only be Post request |
@RequestMapping
can initiate get request or post request
@GetMapping
can only use get request
@PostMapping
can only initiate post request
@Value、@ConfigurationProperties、@PropertySource
annotation | illustrate |
---|---|
@Value | Used to get the properties of the bean, generally used to read the data of the configuration file, acting on the variable |
@ConfigurationProperties | Used to inject Bean properties, and then get the injected value through the current Bean, acting on the class |
@PropertySource | Used to specify the configuration file to be read, can be used with @Value or @ConfigurationProperties |
@PropertySource does not support yml file reading.
@Value
uses the yml configuration file for demonstration here, and the propres configuration file has the same effect. Set the configuration file (dev) of the development environment in the application.yml configuration file, so that what you get with @Value is the configuration file of the development environment. Data, if you switch to the production environment (pro), you will get the data of the production environment
@ConfigurationProperties
This annotation can directly inject the data of the entire class and act on the class
The configuration file is as follows, here use the pro environment
@PropertySource
Note: @PropertySource does not support yml file reading.
The configuration file is as follows: people.properties
test
Of course, @PropertySource can also be used in conjunction with @Value, that is, inject values one by one.
@Configuration、@Bean
@Configuration acts on the class, indicating that this is a configuration class, and @Bean generates a Bean object to join the Spring IOC container
Note: @Configuration is marked on the class, which is equivalent to using this class as the spring xml configuration file, and the function is: configure the spring container (application context)
annotation | illustrate |
---|---|
@Configuration | Acting on the class means that this is a configuration class, which can be understood as the <beans> tag in xml when using spring |
@Bean | Generate a bean object and add it to the container, acting on the method, which can be understood as the label in the xml when using spring |
Create a new configuration class, add User to the container, and customize the life cycle
test
@RequestParam、@RequestBody、@PathVariable、@RequestHeader、@CookieValue
The combination of these annotations is mainly used to receive parameters at the control layer
annotation | illustrate |
---|---|
@RequestParam | Get query parameters. That is, url?name= this form |
@PathVariable | Get path parameters. That is, the form of url/{id}. |
@RequestParam | Get the parameters of the Body, generally used for post to get parameters |
@RequestHeader | Get request header information |
@CookieValue | Get cookie information |
@RequestParam | |
@RequestParam is mainly used to receive the parameters after url?, get or post request, as long as the following url? has parameters, you can get the corresponding parameters |
The @RequestParam annotation has several important attributes, required indicates whether it is required, the default is true, and it must. defaultValue can set the default value of the request parameter. value is the parameter name of the received url (equivalent to the key value).
The sample code is as follows
@GetMapping("/requestParam")
@ResponseBody
public Map<String, String> requestParam(
UserDto userDto,//通过一个实体类来接收,字段名必须一致
@RequestParam(value = "id", required = false) String userId,
@RequestParam(value = "name", required = false) String userName,
@RequestParam(value = "pageIndex", required = true, defaultValue = "1") String pageIndex,
@RequestParam(value = "pageSize", required = true, defaultValue = "5") String pageSize) {
Map<String, String> map = new HashMap<>();
map.put("userDto",userDto.toString());
map.put("id", userId);
map.put("name", userName);
map.put("pageIndex", pageIndex);
map.put("pageSize", pageSize);
return map;
}
Run
@PathVariable.
This annotation is mainly used to obtain path parameters. Parameters in the form of url/{id}/{name} are all available, and get and post requests are all available.
The sample code is as follows:
@PostMapping("/pathVariable/{id}/{name}")
@ResponseBody
public Map<String, String> pathVariable(
@PathVariable(name = "id") String userId,
@PathVariable(name = "name") String userName) {
Map<String, String> map = new HashMap<>();
map.put("id", userId);
map.put("name", userName);
return map;
}
Running result
@RequestBody
This annotation is used to get the request body data (body), get has no request body, so it is generally used for post requests
The sample code is as follows:
@PostMapping("/test01")
@ResponseBody
public UserDto test01(@RequestBody UserDto userDto) {
return userDto;
}
@PostMapping("/test02")
@ResponseBody
public String test02(@RequestBody String str) {
return str;
}
Running results
Note that if you want to pass multiple parameters, you can only encapsulate it into a class in the past. If there are multiple @RequestBody annotations when accessing, a 400 error will be reported. For example, the following code is wrong
@PostMapping("/requestBody")
@ResponseBody
public Map<String,String> requestBody(
@RequestBody(required = true) String id,
@RequestBody(required = true) String name,
@RequestBody(required = false) String sex,
@RequestBody(required = false) String age
){
Map<String,String> map = new HashMap<>();
map.put("id","id");
map.put("name","name");
return map;
}
@RequestHeader
sample code is as follows
@PostMapping("/requestHeader")
@ResponseBody
public String requestBody03(@RequestHeader(name = "Content-Type") String contentType){
return contentType;
}
result
@CookieValue
@GetMapping("/demo3")
public void demo3(@RequestHeader(name = "myHeader") String myHeader,
@CookieValue(name = "myCookie") String myCookie) {
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
}
Come on, program ape!
Original link: https://blog.csdn.net/qq_40298902/article/details/107746642