[Springboot] Summary of commonly used annotations (1)

1. Common notes in detail

@SpringBootApplication: Declare that spring boot will automatically configure the program as necessary. The configuration of @SpringBootApplication is equivalent to: @Configuration , @EnableAutoConfiguration and @ComponentScan three configurations.

@ResponseBody: 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 @esponsebody , 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 in conjunction with @RequestMapping.

@Controller: Used to define the controller class. In the spring project, the controller is responsible for forwarding the URL request sent by the user to the corresponding service interface (service layer). Generally, this annotation is in the class, and usually the method needs to be annotated with @RequestMapping .

@RestController: Used to annotate control layer components (such as actions in struts), a collection of @ResponseBody and @Controller .

@RequestMapping: Provide routing information, responsible for the mapping of URLs to specific functions in the Controller.

@EnableAutoConfiguration: SpringBoot auto-configuration (auto-configuration): Try to automatically configure your Spring application based on the jar dependencies you add. For example, if HSQLDB exists on your classpath, and you have not manually configured any database connection beans, then we will automatically configure an in-memory database". You can add @EnableAutoConfiguration or @SpringBootApplication annotations to a @Configuration Choose automatic configuration on the class. If you find that a specific automatic configuration class that you don't want is applied, you can use the exclusion attribute of the @EnableAutoConfiguration annotation to disable them.

@ComponentScan: Indicates that this class automatically discovers scanning components. It can be understood like this: if you scan for classes annotated with @Component , @Controller , @Service, etc., and register them as Beans, you can automatically collect all Spring components, including @Configuration classes. We often use the @ComponentScan annotation to search for beans and import them in conjunction with the @Autowired annotation. Can automatically collect all Spring components, including @Configuration classes. We often use the @ComponentScan annotation to search for beans and import them in conjunction with the @Autowired annotation. If there is no configuration, Spring Boot will scan the package where the startup class is located and the classes under the sub-packages with @Service, @Repository and other annotations.

@Configuration: It is equivalent to the traditional xml configuration file. If some third-party libraries need to use xml files, it is recommended to still use the @Configuration class as the main configuration class of the project-you can use the @ImportResource annotation to load the xml configuration file.

@Autowired: Automatically import dependent beans, byType. 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.

@Service: Generally used to decorate the components of the service layer.

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

@Bean: The @Bean annotation method is equivalent to the bean configured in XML, meaning that a bean is generated and managed by spring.

@Value: Inject the value of the property configured in Spring boot application.properties.

@Import: Used to import other configuration classes.

@SpringBootApplication: Contains @ComponentScan, @Configuration and @EnableAutoConfiguration annotations.

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

@Configuration: Equivalent to spring's XML configuration file; Java code can be used to check type safety.

@EnableAutoConfiguration: Automatic configuration.

@ComponentScan: Component scanning, which can automatically discover and assemble some Beans.

@Component: It can be used with CommandLineRunner to perform some basic tasks after the program is started.

@RestController: The annotation is a collection of @Controller and @ResponseBody, indicating that this is a controller bean, and the return value of the function is directly filled into the HTTP response body, which is a REST-style controller.

@PathVariable: Get parameters.

@JsonBackReference: Solve the problem of nested external links.

@RepositoryRestResourcepublic: Used in conjunction with spring-boot-starter-data-rest.

@ImportResource: used to load the xml configuration file.

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

@Component: Refers to components in general. We can use this annotation to annotate when the component is not easy to categorize.

@Qualifier: When there are multiple beans of the same type, you can use @Qualifier("name") to specify. Used in conjunction with @Autowired. @Qualifier limited descriptors can be injected according to the name, but can be more fine-grained control how to select candidates.

@Resource(name=”name”,type=”type”): If there is no content in brackets, the default is byName. Do similar things with @Autowired.

Two, JPA annotations

@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 the entity class name are the same, @Table can be omitted.

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

@NoRepositoryBean : Generally used as the repository of the parent class. With this annotation, Spring will not instantiate the repository.

@Column: If the field name is the same as the column name, it can be omitted.

@Id: Indicates that the attribute is the primary key.

@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 it can be switched between multiple databases), and 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 for use, sequenceName is the sequence name of the database, and the two names can be the same.

@Transient: Indicates that this attribute is not a mapping to a field of a database table, and the ORM framework will ignore this attribute. If an attribute is not a field mapping of the database table, it must be marked as @Transient, otherwise, the ORM framework defaults its annotation as @Basic. @Basic(fetch=FetchType.LAZY): The tag can specify the loading method of entity attributes

@JsonIgnore: The function is to ignore some properties in the Java bean when json serialization, serialization and deserialization are affected.

@JoinColumn(name=”loginId”) : One-to-one: A foreign key in this table that points to another table. One-to-many: Another table points to the foreign key of this table.

@OneToOne, @OneToMany, @ManyToOne: corresponding to one-to-one, one-to-many, and many-to-one in the hibernate configuration file.

Three, 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.