The attributes and functions of common annotations

1. @ 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 RequestMapping annotation has six attributes, which are divided into three categories for description below.

value, method
value: Specify the actual address of the request, the specified address can be a specific address, can be dynamically obtained by RestFul, or can be set by regular;

method: Specify the method type of the request, including GET, POST, PUT, DELETE, etc.;

consumes, produces
consumes: Specify the content type of the submitted content (Content-Type) of the processing request, such as application/json, text/html;

produces: specifies the type of content returned, and returns only when the (Accept) type in the request header contains the specified type;

params, headers
params: Specifies that certain parameter values ​​must be included in the request before the method is processed.

headers: The specified request must contain certain specified header values ​​in order for the method to process the request.
2. @ApiOperation and @ApiParam

@ApiOperation is not the annotation that comes with spring is
com.wordnik.swagger.annotations.ApiOperation in swagger  ;

@ApiOperation and @ApiParam are related solutions for the added API. The parameter description is as follows: 
@ApiOperation(value = "interface description", httpMethod = "interface request method", response = "interface return parameter type", notes = "interface release description" ); For other parameters, please refer to the source code; 
@ApiParam(required = "Required parameter", name = "Parameter name", value = "Parameter description")

3、@Transactional

Configuration: https://www.cnblogs.com/xd502djj/p/10940627.html Principle: https://blog.csdn.net/u013929527/article/details/102596243

Spring transaction management is divided into two types: coding style and declarative style. Programming style refers to the realization of transactions through coding. Declarative transactions are based on AOP, which decouples specific business logic and transaction processing. Declarative transaction management prevents business code logic from being polluted, so declarative transactions are used more in actual use. There are two ways of declarative transactions, one is to declare related transaction rules in the configuration file, and the other is based on the @Transaction annotation. This annotation provides the following convenience:
1) According to your configuration, set whether to automatically start the transaction 2) Automatically commit the transaction or roll back when an exception occurs.

4. @Value annotation

Function: Inject constants, values ​​in configuration files, and attribute values ​​of other beans into variables through annotations as the initial values ​​of variables.

4.1 Constant injection

    @Value("normal")

    private String normal; // Inject a normal string

    @Value("classpath:com/config.txt")

    private Resource resourceFile; // inject file resources

    @Value("http://www.baidu.com")

    private Resource testUrl; // Inject URL resources

4.2  bean properties, system properties, expression injection

Form: @Value( "#{}")

Bean attribute injection requires that the injector and the injected person belong to the same IOC container, or the parent-child IOC container relationship, in the same scope.

    @Value("#{beanInject.another}")

    private String fromAnotherBean; // Inject other bean properties: inject the properties of another beanInject object, see the following for the specific definition of the class

    @Value("#{systemProperties['os.name']}")

    private String systemPropertiesName; //Inject operating system properties

    @Value("#{ T(java.lang.Math).random() * 100.0 }")

    private double randomNumber; //Inject expression result

4.3  Configuration file attribute injection

Form @Value("${}")

•Application.properties. application.properties loads this file by default when spring boot starts

• Custom properties file. The custom property file is loaded via @PropertySource. @PropertySource can load multiple files at the same time, or load a single file. If the same key exists in the same first property file and the second property file, the key in the last property file is activated. The path of the loaded file can also be configured with variables, such as ${anotherfile.configinject} as follows. This value is defined in the first property file config.properties

The content of the first property file config.properties is as follows: 
${anotherfile.configinject} as the variable value of the second property file loading path

book.name=bookName

anotherfile.configinject=placeholder

The content of the second properties file config_placeholder.properties is as follows:

book.name.placeholder=bookNamePlaceholder

Guess you like

Origin blog.csdn.net/VABTC/article/details/107529617