SpringBoot annotation analysis and interpretation

The advantage of using annotations:

     1. pure java code, does not require complicated configuration xml file

     2. In the configuration may also enjoy the benefits of object-oriented brought

     3. Type the security of the reconstruction can provide good support

     4. At the same time reduce the complexity of the configuration file can also enjoy the functionality provided by the container springIoC

 

First, the comment Comments (equipped with a sound interpretation) ------ (ctrl + F can be used to search oh ~ ~ ~ ~)

@SpringBootApplication: Affirming let spring boot automatically makes the necessary configuration to the program, this configuration is equivalent to:

@Configuration, @ EnableAutoConfiguration and @ComponentScan three configurations.

@ResponseBody: indicates that the method returns the result directly written HTTP response body is generally used in the asynchronous data acquired for constructing the api RESTful. @RequestMapping after use, the return value is typically resolved as the jump path and, with the results returned @esponsebody not be interpreted as the jump path, but directly written in the HTTP response body. Such as asynchronous get json data, after adding @Responsebody, it will return json data directly. The Notes will generally be used in conjunction with @RequestMapping.

@Controller: a controller for defining classes in the spring by the controller responsible for the project forwards the URL request sent by a user to the corresponding service interface (service layer), generally in the annotation class annotated with the conventional method requires @RequestMapping .

@RestController: a control layer label assembly (e.g., the action struts), @ ResponseBody @Controller and the collection.

@RequestMapping: provides routing information, URL mapping to the Controller responsible for specific functions.

@EnableAutoConfiguration: SpringBoot automatic configuration (auto-configuration): attempts to automatically configure your application based on Spring jar rely on your add. For example, if your classpath under HSQLDB, and you do not have to manually configure any database connections beans, then we will automatically configure a type of memory (in-memory) database. "You can add notes to a @EnableAutoConfiguration or @SpringBootApplication @Configuration class up select automatic configuration. If you find that a particular application automatically configures classes you do not want, you can use negative attributes @EnableAutoConfiguration comment to disable them.

@ComponentScan: indicates the type of automatic discovery scanning assembly. Personal understanding is equivalent to, if the class has to scan these annotations @ Component, @ Controller, @ Service, etc., and registered as Bean, Spring can automatically collect all the components, including @Configuration class. We often use @ComponentScan annotation search beans, combined with @Autowired notes import. Spring can automatically collect all the components, including @Configuration class. We often use @ComponentScan annotation search beans, combined with @Autowired notes import. If no words, Spring Boot will start scanning the next class pack and use the @ Service, @ Repository annotation and other sub-package in the class lies.

@Configuration: equivalent to the traditional xml configuration file, if you need to use some third-party library xml file, still recommended as the main class configuration items by @Configuration class - can be used to load xml configuration file @ImportResource comment.

@Import: used to introduce other configuration class.

@ImportResource: used to load xml configuration file.

@Autowired: automatically import dependent bean

@Service: General assembly of the service layer for modifying

@Repository: Use @Repository annotation can ensure DAO abnormal or repositories provide translation, this annotation modified or repositories DAO class will be found and configured ComponetScan, also do not need to provide XML configuration items for them.

@Bean: Tagging with @Bean equivalent bean XML configured.

@Value: injecting value Spring boot application.properties configuration attributes. Sample code:

@Inject: equivalent to the default @Autowired, but there is no required attributes;

@Component: refers to the components when the components are classified bad, we can use this annotation to mark.

@Bean: XML equivalent of the above, in the method, rather than a class, meaning that produces a bean, and manage to spring.

@AutoWired: automatically import dependent bean. byType way. Bean configured to make use, completed properties, assembly method, it can be labeled class fields, methods and constructors, the completion of the automatic assembly. When coupled with (required = false), even if can not find the bean is not an error.

@Qualifier: when a plurality of the same type of Bean, can be specified @Qualifier ( "name"). Used in conjunction with @Autowired. In addition to defining @Qualifier descriptor name according to the injection, but can be more fine-grained control how the selected candidates, specifically used as follows:

@Resource (name = "name", type = "type"): there is no content within the parentheses, the default byName. A similar thing with @Autowired dry.

Second, the notes are listed below

@SpringBootApplication: contains @ ComponentScan, @ Configuration and @EnableAutoConfiguration comment. among them

@ComponentScan: Let spring Boot Configuration class to scan and add it to the program context.

@Configuration: equivalent to the spring XML configuration file; the use of Java code can check the type of security.

@EnableAutoConfiguration: automatic configuration.

@ComponentScan: scanning assembly, and assembling a number of automatic discovery Bean.

@Component can be used with CommandLineRunner used to perform some basic tasks after the program starts.

@RestController: the annotations are @Controller @ResponseBody and collection, the controller indicates this is the bean, and the return value of the function is filled directly into the body of the HTTP response, the controller is REST style.

@Autowired: automatic import.

@PathVariable: acquisition parameters.

@JsonBackReference: nested outside the chain to solve the problem.

@RepositoryRestResourcepublic: with the spring-boot-starter-data-rest used.

Three, JPA annotations

@Entity: @Table (name = ""): indicates that this is an entity class. Both generally used jpa annotations using a general, but if the table name and the name of the entity, then the same class, @ Table may be omitted

@MappedSuperClass: is used in the determination of the parent entity. Properties sub-class of the parent class can be inherited.

@NoRepositoryBean: generally used as the parent class repository, this annotation, spring not to instantiate the repository.

@Column: field name and column names the same, it may be omitted.

@Id: indicates that the primary key attribute.

@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "repair_seq"): is a front key generation strategy sequence (may be Auto, IDENTITY, native, etc., represents Auto switch between a plurality of databases), the name of the specified sequence is repair_seq.

@SequenceGeneretor (name = "repair_seq", sequenceName = "seq_repair", allocationSize = 1): name is the name of the sequence to use, the name of the database sequence is sequenceName, two names may coincide.

@Transient: indicates that the property is not mapped to a field in a database table, ORM framework will ignore the attributes. If a property is not a field mapping database tables, be sure to mark it as @Transient, otherwise, ORM framework annotated as its default @Basic. @Basic (fetch = FetchType.LAZY): loading the entity may specify tag attributes

@JsonIgnore: the role is json serialized Java bean will ignore some of the properties, serialization and de-serialization are affected.

@JoinColumn (name = "loginId"): one: This table to the foreign key in another table. Many: foreign key to another table in this table.

@ OneToOne, @ OneToMany, @ ManyToOne: hibernate configuration file corresponds to one to one, one to many, many to one.

Four, springMVC related notes

@RequestMapping: @RequestMapping ( "/ path" ) indicates that the controller handles all "/ path" of the request UR L. RequestMapping is a process for annotation request address mapping, it can be used for class or method.
For the class, all the methods in response to the request class are represented in the address as the parent path. This note has six attributes:
params: Specifies the request must contain certain parameter values, before allowing the processing method.
headers: Specifies the request must contain certain specified header value, in order for this method to process the request.
value: actual address assignment request, the designated address may be a URI Template mode
method: method specifies the type of request, GET, POST, PUT, DELETE and so
consumes: Submit content type (Content-Type) specified processing request, such as application / JSON, text / HTML;
Produces: content type of the returned only when the head (the Accept) the request includes a request type specifies the type will return to

@RequestParam : parameters used in the previous method.
@RequestParam
String request.getParameter A = ( "A").

@PathVariable: path variable. Such as

Parameter name and braces just have to be the same.

Fifth, global exception handler

@ControllerAdvice: contains @Component. It can be scanned. Unified handle exceptions.

@ExceptionHandler (Exception.class): represents encountered this exception is to perform the following method is used in the above methods.

Sixth, the project specific configuration parsing and environment

@MappedSuperclass:
1. @ MappedSuperclass annotated with a parent class above, is used to identify the parent class

Class 2. @ MappedSuperclass identified said it can not be mapped to a database table, because it is not a complete entity class, but it has attributes can be mapped in a subclass of database tables used in

Class 3. @ MappedSuperclass identified or can not have @Entity @Table comment

@Column:

1. @Column required before the database table when the properties of the entity mapped thereto a different name column labeled described, this property is usually placed entity attribute declaration statement, and may also be used with labeled @Id.

2. @ Column marked common properties be name, or for setting a mapping database table column name. In addition, the label further comprises a plurality of additional attributes, such as: unique, nullable, length, precision and the like. details as follows:

  1 name attributes: name attribute defines the name of the field in the database is marked in the table corresponding field

  2 unique attributes: unique attribute indicates whether the field is a unique identifier defaults to false, if there is a need to uniquely identify the field, you can either use the tag may be used in the annotation @Table @UniqueConstraint

  3 nullable properties: nullable attribute indicates whether the field can be null value, the default is true

  4 insertable properties: insertable attribute indicates when using "INSERT" statement to insert data, whether the value of the field need to insert

  5 updateable properties: updateable attribute indicates when using the "UPDATE" statement to insert the data, need to update the value of the field

  6 insertable and updateable properties: generally used for read-only attribute, for example, primary and foreign keys and the like, these fields are typically generated automatically

  7 columnDefinition properties: columnDefinition attribute indicates create the table, create SQL statements that field, generally used when a table definition by using the Entity, if the database table has been built, the property is not necessary to use

  8 table attributes: table attribute defines the name of the table containing the current field

  9 length properties: length property represents the length field when the type field is varchar, the attribute is valid, the default is 255 characters

 10 precision scale properties and attributes: precision scale properties and properties together represent precision, when the field type is double, precision of the total length value, scale the number of bits occupied by the decimal point

 

    As follows:
   1.double types are mapped in the database for the double type, and scale properties Precision invalid
   2.double type if the specified numeric type columnDefinition attribute specified as a decimal precision and is subject to final columnDefinition
   3.BigDecimal type database mapping type decimal, precision and scale properties effective
   4.precision scale and only valid attribute types BigDecimal

3. @ Column annotation columnDefinition properties: This field indicates the actual type in the database generally ORM framework can automatically determine the type field in the database according to the attribute types, but still can not be determined for the Date type database field type whether DATE, TIME. or TIMESTAMP. Moreover, String default mapping type is VARCHAR, BLOB or TEXT field types for a particular database if you want to map to the String type.

4. @ Column before labeling methods may also be placed getter properties

@Getter and @Setter (Lombok)

@Setter: annotations on attributes; @Getter provided a method of setting attributes: annotation on the property; getting property provides a method for the

  1 @Data: annotation on the class; Getting class provides all of the attributes and setting method, also provided equals, canEqual, hashCode, toString Method
  2 
  . 3 @Setter: annotations on attribute; property provides for the setting method
  . 4 
  . 5 @Getter : annotation on the property; getting provides a method for the property
  . 6 
  . 7 @ Log4j2: annotation on class; a property of the class named log4j log log objects, and the like annotation @ log4j
  . 8 
  . 9 @NoArgsConstructor: annotation on the class; is class provides a no-argument constructor
 10 
 . 11 @AllArgsConstructor: annotation on the class; to provide a full class constructor parameter
 12 is 
 13 is @EqualsAndHashCode: By default, all non-transitory use (non-transient) and non-static (non-static) and equals hascode field generating methods, can be used to specify which particular attributes.
 14 
 15 @toString: generating toString method, by default, will output class names, all attributes, output attributes in the order, separated by commas.
 16 
 17 @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
 18 no-argument constructor, some parameters constructor, the whole argument constructor, when we need more overloaded constructor, our only handwritten
 19 
 20 @NonNull: Notes on the property, if the notes, it must not be Null
 21 
 22 @val: Notes on the property, if the annotation is set to the final type, comments can view the source code to know

 

When you perform a variety of methods of persistence, the state entity will change, change of state will lead to different life cycle events. These events can use a different comment character to indicate the callback function occurs.

@ Javax.persistence.PostLoad: after loading.

@ Javax.persistence.PrePersist: persistence ago.

@ Javax.persistence.PostPersist: persistence after.

@ Javax.persistence.PreUpdate: before updating.

After updating: @ javax.persistence.PostUpdate.

@ Javax.persistence.PreRemove: before deleting.

@ Javax.persistence.PostRemove: After deleting.

1) database query

@PostLoad events triggered in the following cases:

Performing the EntityManager.find () or getReference () method of an entity after loading.

After JPQL query execution.

EntityManager.refresh () method gets called.

2) inserted into the database

@PrePersist and @PostPersist event in the entity object into the database occurs during:

@PrePersist the incident immediately after the call to persist () method, then the data has not really inserted into the database.

@PostPersist event occurs after the data has been inserted into the database.

3) Database Update

@PreUpdate @PostUpdate and trigger events caused by the update entity:

@PreUpdate event before state entities synchronized to the database trigger, this time the data has not really been updated to the database.

@PostUpdate event after the state entity to database synchronization trigger, synchronization occurs when the transaction commits.

4) Delete database

@PreRemove @PostRemove and trigger events caused by the deletion entities:

@PreRemove event is fired before the entity from the database is deleted, calling remove () method removes occurs when, at this time the data is not really deleted from the database.

@PostRemove event is triggered after the entity is deleted from the database.
2019-11-0300: 28: 17

Guess you like

Origin www.cnblogs.com/itboxue/p/11784929.html