1. Annotations list
@SpringBootApplication: Contains @ComponentScan, @Configuration and @EnableAutoConfiguration annotations. Where @ComponentScan lets spring Boot scan to the Configuration class and add it to the program context.
@Configuration is equivalent to spring's XML configuration file; Java code can check type safety.
@EnableAutoConfiguration Automatic configuration.
@ComponentScan ComponentScan component scans to automatically discover and assemble some beans.
@Component can be used with CommandLineRunner to execute some basic tasks after the program is started.
The @RestController 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. It is a REST-style controller.
@Autowired automatically import.
@PathVariable gets the parameters.
@JsonBackReference solves nested external link problem.
@RepositoryRestResourcepublic is used with spring-boot-starter-data-rest.
2. Detailed explanation of annotations
@SpringBootApplication: Declare spring boot to automatically configure the program with necessary configuration. This configuration is equivalent to three configurations: @Configuration, @EnableAutoConfiguration and @ComponentScan.
package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }@ResponseBody: It means that the return result of this method is written directly into the HTTP response body. It is generally used when data is obtained 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 will be directly written into the HTTP response body. For example, if you get json data asynchronously and add @responsebody, the json data will be returned directly. This annotation will generally be used with @RequestMapping. Sample code:
@RequestMapping("/test") @ResponseBody public String test(){ return "ok"; }@Controller: is 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. Usually, the method needs to be coordinated with the annotation @RequestMapping. Sample code:
@Controller @RequestMapping("/demoInfo") publicclass DemoController { @Autowired private DemoInfoService demoInfoService;@RequestMapping("/hello")public String hello(Map<String,Object> map){ System.out.println("DemoController.hello()"); map.put("hello","from TemplateController.helloHtml"); //The hello.html or hello.ftl template will be used for rendering and display. return"/hello";}}@RestController: A collection of control layer components (such as action in struts), @ResponseBody and @Controller. Sample code:
package com.kfit.demo.web;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/demoInfo2") publicclass DemoController2 {@RequestMapping("/test")public String test(){ return "ok";}}@RequestMapping: Provides routing information and is responsible for mapping URLs to specific functions in Controller.
@EnableAutoConfiguration: Spring Boot Auto-configuration: Try to automatically configure your Spring application based on the jar dependencies you added. For example, if HSQLDB exists under your classpath and you have not manually configured any database connection beans, then we will automatically configure an in-memory database. You can add the @EnableAutoConfiguration or @SpringBootApplication annotation to a @Configuration class to select autoconfiguration. If you find that a specific autoconfiguration class you do not want is applied, you can use the exclusion attribute of the @EnableAutoConfiguration annotation to disable them.
@ComponentScan: means that the class will automatically discover the scanning component. Personal understanding is equivalent to that if you scan a class with annotations such as @Component, @Controller, @Service, etc. and register it as a bean, you can automatically collect all Spring components, including the @Configuration class. We often use @ComponentScan annotation to search for beans and import them in combination with @Autowired annotation. All Spring components can be automatically collected, including the @Configuration class. We often use @ComponentScan annotation to search for beans and import them in combination with @Autowired annotation. If there is no configuration, Spring Boot will scan the classes under the package where the startup class is located and the subpackage that use the annotations of @Service, @Repository, etc.
@Configuration: equivalent to a 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.
@Import: Used to import other configuration classes.
@ImportResource: Used to load the xml configuration file.
@Autowired: Automatically import dependent beans
@Service: Components that are generally used to modify 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 them with XML configuration items.
@Bean: Use the @Bean annotation method to be equivalent to the bean configured in XML.
@Value: Inject the value of the property configured by Spring boot application.properties. Sample code:
@Value(value = "#{message}") private String message;@Inject: equivalent to the default @Autowired, but without the required attribute;
@Component: refers to components in general. When components are not easy to classify, we can use this annotation to annotate.
@Bean: It is equivalent to XML, placed on top of a method, not a class, which means to generate a bean and hand it over to spring management.
@AutoWired: Automatically import dependent beans. byType method. Use the configured beans to complete the assembly of properties and methods. It can mark class member variables, methods and constructors to complete the automatic assembly work. When (required=false) is added, an error will not be reported even if the bean cannot be found.
@Qualifier: When there are multiple beans of the same type, you can use @Qualifier("name") to specify it. Works with @Autowired. In addition to injecting according to the name, @Qualifier qualifier descriptors can be used to perform finer granularity controls how to select candidates. The specific usage method is as follows:
@Autowired @Qualifier(value = "demoInfoService") private DemoInfoService demoInfoService;
@Resource(name=”name”,type=”type”): If there is no content in brackets, the default is byName. Do something similar to @Autowired.
3. JPA notes
@Entity: @Table(name=”"): Indicates that this is an entity class. Generally used for jpa's two annotations, but if the table name and entity class name are the same, @Table can be omitted.
@MappedSuperClass: Used to determine the entity that is the parent class. The attribute subclass of the parent class can be inherited.
@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: means that this attribute is the primary key.
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "repair_seq"): means that the primary key generation strategy is sequence (can be Auto, IDENTITY, native, etc., Auto means that it can switch 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, so that sequenceName is the sequence name of the database, and the two names can be consistent.
@Transient: means that this property is not a map to a field in the database table, and the ORM framework will ignore this property. If an attribute is not a field map of a database table, it must be marked as @Transient. Otherwise, the ORM framework defaults to its annotation as @Basic. @Basic(fetch=FetchType.LAZY): The tag can specify how entity attributes are loaded
@JsonIgnore: The function is to ignore some properties in the Java bean when serializing json, and both serialization and deserialization are affected.
@JoinColumn(name=”loginId”): One-to-one: Foreign keys in this table pointing 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.
4. SpringMVC related notes
@RequestMapping: @RequestMapping("/path") means that the controller handles all URL requests for "/path". RequestMapping is an annotation used to handle request address mappings and can be used on classes or methods.
For use on a class, all methods that represent that response requests in a class take this address as the parent path. This annotation has six attributes:
@RequestParam: Used before the parameter of the method.
@RequestParam String a =request.getParameter("a"). @PathVariable:PathVariable. For example, RequestMapping("user/get/mac/{macAddress}") public String getByMacAddress(@PathVariable String macAddress){ //do something; }The parameters must be the same as the names in the braces.
5. Global exception handling
@ControllerAdvice: Contains @Component. Can be scanned. Unified exception handling.
@ExceptionHandler(Exception.class): Used on the method to indicate that if you encounter this exception, you will execute the following method.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.