【Recommended tutorial: spring tutorial】
springboot common annotations
1, @SpringBootApplication
Includes @Configuration, @EnableAutoConfiguration, @ComponentScan
is usually used in the main class superior.
2. @Repository
is used to mark data access components, that is, DAO components.
3. @Service
is used to mark business layer components.
4. @RestController
is used to mark control layer components (such as actions in struts), including @Controller and @ResponseBody
5. @ResponseBody
means that the return result of this method is directly written into the HTTP response body
is generally used when obtaining data asynchronously. After using @RequestMapping, The return value is usually parsed as a jump path. After adding @responsebody, the return result will not be parsed
as a jump path, but will be written directly into the HTTP response body. For example, if you obtain json data asynchronously and add @responsebody, the json data will be returned directly.
6. @Component
refers to components in general. When components are difficult to classify, we can use this annotation to annotate them.
7, @ComponentScan
Component scan. Equivalent to 611dfb857b03b245adf9fd6566a6e09a, if classes with @Component @Controller @Service and other annotations are scanned,
these classes will be registered as beans.
8. @Configuration
points out that this class is the information source of Bean configuration, which is equivalent to 3c9fdce8078791299ca92e41bddc2b8fa6748f3712e7c0e1f60ffca24b3db7b0 in XML. It is usually added in On the main category.
9. @Bean
is equivalent to 60e23eb984d18edbb092da6b8f295aba4bb0e59fd50cbfe6f6ce2215b9d94243 in XML. It is placed above the method instead of the class, which means to generate A bean and managed by spring.
10. @EnableAutoConfiguration
Let Spring Boot automatically configure the Spring framework based on the dependencies declared by the application, usually added to the main class.
11, @AutoWired
byType method. Use the configured beans to complete the assembly of properties and methods. It can annotate class member variables, methods and constructors to complete automatic assembly.
When (required=false) is added, no error will be reported even if the bean cannot be found.
12. @Qualifier
When there are multiple beans of the same type, you can use @Qualifier("name") to specify. Used in conjunction with @Autowired
13, @Resource(name="name",type="type")
If there is no content in brackets, byName will be used by default. Do similar things with @Autowired.
14. @RequestMapping
RequestMapping is an annotation used to handle request address mapping and can be used on classes or methods. Used on a class, it means that all methods in the class that respond to requests use this address as the parent path.
This annotation has six attributes:
params: Specify that the request must contain certain parameter values before it can be processed by this method.
headers: The specified request must contain certain specified header values in order for this method to process the request.
value: Specify the actual address of the request, the specified address can be a URI Template mode
method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.
consumes: Specifies the submitted content type (Content-Type) for processing the request, such as application/json, text/html;
produces: Specifies the returned content type, only when the (Accept) type in the request header
#15 is returned only if it contains the specified type. @RequestParam
is used in front of the parameters of the method.
@RequestParam String a =request.getParameter("a")。
16, @PathVariable
Path variable. The parameters must be the same as the names in the curly brackets.
RequestMapping("user/get/mac/{macAddress}") public String getByMacAddress(@PathVariable String macAddress){ //do something; }
17, @Profiles
Spring Profiles provide a way to isolate application configurations and make these configurations only effective in specific environments.
Any @Component or @Configuration can be marked with @Profile, thereby limiting the timing of loading it.
@Configuration @Profile("prod") public class ProductionConfiguration { // ...}
18, @ConfigurationProperties
Spring Boot will try to verify external configuration, using JSR-303 by default (if it is in the classpath).
You can easily add JSR-303 javax.validation constraint annotations to your @ConfigurationProperties class:
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { @NotNullprivate InetAddress remoteAddress; // ... getters and setters }
Global exception handling
@ControllerAdvice
Contains @Component. can be scanned.
Uniform handling of exceptions
@ExceptionHandler (Exception.class):
Used above the method to indicate that the following method will be executed when encountering this exception
Summary: The above is the entire content of this article, I hope it will be helpful to everyone.
The above is the detailed content of What are the common annotations for springboot?. For more information, please follow other related articles on the PHP Chinese website!