search
HomeCommon ProblemWhat are the common annotations for springboot?

What are the common annotations for springboot?

Apr 12, 2019 pm 02:17 PM
springbootCommon annotations

What are the common annotations for springboot?

【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 , 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 in XML. It is usually added in On the main category.

9. @Bean

is equivalent to 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.