Spring boot annotations

Source: Internet
Author: User

@RequestMapping provide routing information.

The @Controller is used to define the controller class, which in the spring project is the responsibility of the controller to forward URL requests from the user to the corresponding service interface (services layer), generally this annotation is in the class, The usual method needs to mate with the annotation @requestmapping.

@ResponseBody indicates that the return result of the method is written directly to the HTTP response body.

Typically used when fetching data asynchronously, after using @requestmapping, the return value usually resolves to a jump path, plus
After @responsebody, the returned results are not resolved to the jump path, but are written directly to the HTTP response body. Like what
Asynchronously gets the JSON data, plus @responsebody, which returns the JSON data directly.

@RestController tells Spring to render the result as a string and return it directly to the caller.

is a collection of @controller and @responsebody, which indicates that this is a controller bean, and that it is a restful controller that fills the return value of the function directly into the HTTP response body.

Note: The Restcontroller and requestmapping annotations are spring MVC annotations, not a specific part of spring boot.

@EnableAutoConfiguration This note tells spring boot to guess how you want to configure spring based on the added jar dependency.

Because Spring-boot-starter-web adds Tomcat and spring MVC, Auto-configuration will assume that you are developing a web app and setting up spring accordingly.

Starter POMs and Auto-configuration: The purpose of design auto-configuration is to better use "Starter POMs", but these two concepts are not directly linked. You are free to choose a jar dependency other than starter Poms, and spring boot will still do its best to automatically configure your app.

If you find that you have applied specific auto-configuration classes that you do not want, you can disable them by using the exclusion properties of the @EnableAutoConfiguration annotations.

@EnableAutoConfiguration (Exclude={datasourceautoconfiguration.class})

@AutoWired 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 annotate, complete the automatic assembly construction work.

When added (Required=false), even if the bean can not find the error.

@Component a generic component. We can use this annotation for labeling when the component is poorly categorized. General public methods I will use this annotation.

@RequestParam used in front of the parameters of the method.

@RequestParam String a =request.getparameter ("a");

@PathVariable Path variable.

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

@value Annotations to read the configuration inside the Application.properties

1 # face++23 face_api_secret =d9wuqgcylvocidsbx35uth********
1 @Value ("${face_api_key}")     2private  String api_key;        3 @Value ("${face_api_secret}")    4private String Api_secret;

Note: When using this annotation, a class that uses @value, if referenced by another class as an object, must use the injection method instead of new. This is important, and I'm just being caught in this hole.

@ExceptionHandler (exception.class): Use the method above to indicate that you have encountered this exception to perform the following methods.

@ControllerAdvice contains @component. can be scanned to. Handles exceptions uniformly.

@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 ("Production"publicclass//  ... }

Loads an XML configuration file @ImportResource annotations.

@SpringBootApplication This annotation is equivalent to using @Configuration with default properties, @EnableAutoConfiguration and @ComponentScan. assert that spring boot automatically configures the program as necessary.

Many spring boot developers always use @Configuration, @EnableAutoConfiguration and @ComponentScan annotate their main class. Because these annotations are used so frequently (especially if you follow these best practices), Spring boot provides a convenient @SpringBootApplication choice. Where @componentscan let spring boot scan to the configuration class and add it to the program context.

@Configuration an XML configuration file that is equivalent to spring and can be checked for type safety using Java code.

@ComponentScan component scans to automatically discover and assemble some beans.

@Service components typically used to decorate the Service layer

@Repository use @repository annotations to ensure that DAO or repositories provides exception translation, and that this annotation-decorated DAO or repositories class is Componetscan discovered and configured, There is also no need to provide them with XML configuration items.

@Entity: @Table (name= ""): Indicates that this is an entity class. Typically used for JPA these two annotations are generally used, but if the table name and entity class name are the same, @Table can omit

@Column: If the field name is the same as the column name, you can omit it.

@Id: Indicates that the property is a primary key

Spring boot 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.