Some commonly used annotations and usage of springboot

@Bean (destroyMethod = "close" ) //destroy-method="close" The function of destroy-method="close" is to put the connection back into the data pool when the database connection is not in use to facilitate the next call.

@Value("")//Get the property value in the application.properties file

@Component("")//Instantiate ordinary pojo into the spring container, which is equivalent to <bean id="" class=""> in the configuration file

@Service //Used to represent the business layer

@Repository //Used to mark database access components, that is, the dao layer

@RestController //i.e. @ResponseBody and @Controller, used to annotate the control layer

@ConfigurationProperties
Spring Boot will try to verify the 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 {
@NotNull
private InetAddress remoteAddress;
// ... getters and setters
}

@Profiles
Spring Profiles provides a way to isolate application configuration and make these configurations only take effect in specific environments.

Any @Component or @Configuration can be marked with @Profile, thereby limiting the time to load it.

@Configuration
@Profile("prod")
public class ProductionConfiguration {
    // ...
}

@PathVariable:


Path variable. The parameter must be the same as the name in the braces

RequestMapping("user/get/mac/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress){
  //do something;
}

 

@RequestMapping:
RequestMapping is an annotation used to process request address mapping, which can be used on classes or methods. Used on classes, it means that all methods in the class that respond to requests use this address as the parent path.
The annotation has six attributes:
params: specify that the request must contain certain parameter values ​​before the method is processed.
headers: The specified request must contain certain specified header values ​​in order for the method to process the request.
value: specify the actual address of the request, the specified address can be URI Template mode
method: specify the method type of the request, GET, POST, PUT, DELETE, etc.
Consumes: specify the content type (Content-Type) of the processing request, such as application/ json,text/html;
produces: specifies the content type to be returned. Only when the (Accept) type in the request header contains the specified type, will it return


@RequestParam: used in front of the method parameters.

@RequestParam String a =request.getParameter("a")。

 

Global exception handling

@ControllerAdvice: Contains @Component. Can be scanned. Handle exceptions uniformly.

@ExceptionHandler(Exception.class): Used on the method to indicate that the following method will be executed when this exception is encountered.

@EnableAutoConfiguration:
Let Spring Boot automatically configure the Spring framework based on the dependencies declared by the application, generally added to the main class.

@AutoWired:
byType method. Use the configured Bean to complete the assembly of attributes and methods. It can annotate class member variables, methods and constructors to complete the work of automatic assembly.
When (required=false) is added, no error will be reported even if the bean is not found.

@Qualifier:
When there are multiple beans of the same type, you can use @Qualifier("name") to specify. Use @Resource(name="name",type="type") in conjunction with @Autowired

:

If there is no content in brackets, the default is byName. Do similar things with @Autowired.

@EnableTransactionManagemetn(proxyTargetClass =true) // Turn off the JDK dynamic proxy mechanism

 

 

Guess you like

Origin blog.csdn.net/pengyiccb/article/details/80902206