Annotation-study notes

Advantages of using annotations:

  • Using pure java code, no need to configure complicated xml files
  • You can also enjoy the benefits of object-oriented configuration in the configuration
  • Type safety can provide good support for refactoring
  • While reducing complex configuration files, you can also enjoy the functions provided by the springIoC container

1、SpringBoot/spring

  1. @SpringBootApplication

Affirm that spring boot automatically configures the program as necessary. This configuration is equivalent to: @Configuration, @EnableAutoConfiguration and @ComponentScan three configurations.

  1. @Repository

Indicates that the return result of this method is directly written into the HTTP response body, which is generally used when obtaining data asynchronously, and is used to build a RESTful api. 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 is directly written into the HTTP response body. For example, to obtain json data asynchronously, after adding @Responsebody, the json data will be returned directly. This annotation is generally used together with @RequestMapping.

  1. @Service

Components used to decorate the service layer

  1. @Controller

Components used to decorate the controller layer

  1. @Autowired

Automatically import dependent beans. 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 automatic assembly. Add (required=false), no error will be reported even if bean is not found

  1. @Repository

Using @Repository annotation can ensure that DAO or repositories provide exception translation. DAO or repositories classes modified by this annotation will be discovered and configured by ComponetScan, and there is no need to provide XML configuration items for them

  1. @Component

Refers to components in general. When the components are not well classified, we can use this annotation to mark

  1. @Bean

It is equivalent to XML, placed above the method, not the class, which means that a bean is generated and handed over to spring for management

  1. @Inject

Equivalent to the default @Autowired, except that there is no required attribute

  1. AthPathVariable

Get parameters

  1. @Qualifier

When there are multiple beans of the same type, you can use @Qualifier("name") to specify. Used in conjunction with @Autowired. @Qualifier qualified descriptor can be injected according to the name

  1. @Resource(name=”name”,type=”type”):

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

2、Jpa

  1. @Entity:
    @Table(name=”“):

Indicates that this is an entity class. Generally used in jpa, these two annotations are generally used together, but if the table name and entity class name are the same, @Table can be omitted

  1. @MappedSuperClass

Used to determine the entity that is the parent class. The properties of the parent class can be inherited by subclasses

  1. @Column

If the field name is the same as the column name, you can omit

  1. @Id

Indicates that the attribute is the primary key

  1. @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = “repair_seq”)

Indicates that the primary key generation strategy is sequence (it can be Auto, IDENTITY, native, etc., Auto means that you can switch between multiple databases), and the name of the specified sequence is repair_seq

4. SpringMVC is concerned about the solution

  1. @RequestMapping

@RequestMapping("/path") indicates that the controller processes all URL requests of "/path". RequestMapping is an annotation used to process request address mapping, which 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. 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, and only returns if the specified type is included in the (Accept) type in the request header

  1. @RequestParam

Used in front of method parameters

  1. @PathVariable path variable

@PathVariable
String a =request.getParameter(“a”)

  1. AthPathVariable

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

4. Global exception handling

  1. @ControllerAdvice

Contain @Component. Can be scanned. Unified exception handling

  1. @ExceptionHandler(Exception.class)

Used on the method to indicate that the following method is executed when this exception is encountered

Guess you like

Origin blog.csdn.net/weixin_46687295/article/details/107873642