Springboot frequently used annotations

Source: Internet
Author: User

@SpringBootApplication:

Contains @configuration, @EnableAutoConfiguration, @ComponentScan
Typically used on the main class.

@Repository:
Used to label data access Components, or DAO components.

@Service:
Used to label business layer components.

@RestController:
Used to label control-layer components, such as action in struts, including @controller and @responsebody.

@ResponseBody:
Indicates that the return result of the method is written directly to the HTTP response body
Generally used when retrieving data asynchronously, after using @requestmapping, the return value is usually resolved to the jump path, plus @responsebody returns the result will not be resolved to the jump path, but directly to the HTTP response body. For example, asynchronously fetching JSON data, plus @responsebody, will return the JSON data directly.

@Component:
We can use this annotation to annotate components when they are poorly categorized.

@ComponentScan:
Component Scan. Personal understanding is equivalent to <CONTEXT:COMPONENT-SCAN> If you scan a class that has @component @Controller @Service such annotations, register these classes as beans.

@Configuration:
It is indicated that this class is the information source of Bean configuration, which is equivalent to <beans></beans&gt in XML, and is usually added to the main class.

@Bean:
Equivalent to the &LT;BEAN&GT;&LT;/BEAN&GT in XML; put it on top of the method, not the class, meaning to produce a bean and give it to spring to manage.

@EnableAutoConfiguration:
let spring Boot automatically configure the spring framework based on the dependencies declared by the application, typically on the main class.

@AutoWired:
Bytype Way. The configuration of the bean to use, the completion of the properties, methods of assembly, it can be the class member variables, methods and constructors to label, complete the work of automatic assembly.
when added (Required=false), even if the bean can not find the error.

@Qualifier:
when you have more than one bean of the same type, you can specify it with @qualifier ("name"). Use with @autowired

@Resource (name= "name", type= "type"):
without the bracketed content, the default byname. A similar thing to @autowired do.

@RequestMapping:

Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address.
The note has six properties:
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Value: Specifies the actual address of the request, the specified address can be URI Template mode
Method: Specifies the type of method requested, GET, POST, PUT, delete, etc.
Consumes: Specifies the type of submission to process the request (Content-type), such as application/json,text/html;
Produces: Specifies the type of content returned, only if the type in the request header (Accept) contains the specified type to return

@RequestParam:
Used in front of the method's parameters.
@RequestParam String a =request.getparameter ("a").

@PathVariable:

The PATH variable. The arguments are the same as the names in the curly braces.

Requestmapping ("user/get/mac/{macaddress}")public string getbymacaddress (@PathVariable string macAddress) { //dosomething;}    

@Profiles
Spring profiles provides a way to isolate application configurations and allow these configurations to take effect only in a specific environment.
Any @component or @configuration can be @profile labeled, limiting the timing of loading it.

@Configuration @profile ("prod")class productionconfiguration {    // ...}    

@ConfigurationProperties
Spring Boot will attempt to validate the external configuration by default with JSR-303 (if in the Classpath path).
You can easily add JSR-303 javax.validation constraint annotations to your @configurationproperties class:

@Component @configurationproperties (prefix= "Connection")class connectionsettings {@NotNullprivate inetaddress remoteaddress; // ... getters and setters}      

Global exception Handling

@ControllerAdvice:
Contains @component. can be scanned to.
Handles exceptions uniformly.

@ExceptionHandler (Exception.class):
Use the method above to indicate that you are encountering this exception by performing the following methods.

Springboot frequently used annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.