Send you a SpringBoot notes Guinness, please check! - end of the text to send books

Original link: http://product.dangdang.com/27921482.html

Click on the top [ full-stack developer community ]upper right corner [...][set to star ⭐ ]

640?

Author: Zhang Boyi

https://blog.csdn.net/zhanglong_4444


table of Contents

First, notes (annotations) list

Second, notes (annotations) Comments

Three, JPA annotations

Four, springMVC related notes

Fifth, global exception handler


First, notes (annotations) list

@SpringBootApplication: contains @ ComponentScan, @ Configuration and @EnableAutoConfiguration comment. Which @ComponentScan scan to make spring Boot Configuration class 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 assembly scans a number of automatic discovery and assembly Bean.

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

@RestController annotations are @Controller @ResponseBody and the 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 automatically imported.

@PathVariable acquisition parameters.

@JsonBackReference resolve the issue outside the chain of nested.

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


Second, notes (annotations) Comments

@SpringBootApplication: Affirming let spring boot automatically makes the necessary configuration to the program, this configuration is equivalent to: @Configuration, @ EnableAutoConfiguration and @ComponentScan three configurations.

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: 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 @responsebody 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.

Sample code:

@RequestMapping(“/test”)
@ResponseBody
public String test(){
    return”ok”;
}

@Controller: used to define the controller class, the spring project by a controller responsible for forwarding 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 .

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");
        //会使用hello.html或者hello.ftl模板进行渲染显示.
        return"/hello";
    }
}

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

Sample code:

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, URL mapping to the Controller responsible for specific functions.

@EnableAutoConfiguration: Spring the Boot automatic configuration (auto-configuration): attempts to automatically configure your Spring application according to jar you depend added. 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 to 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: with @Bean bean XML tagging method is equivalent to the configuration.

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

Sample code:

@Value(value = “#{message}”)
private String message;

@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: equivalent to the above XML, on the approach, 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:

@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;

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


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: used in determining whether 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"): represents a primary key generation strategy is 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 : corresponds to hibernate configuration file 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.

  • 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: Specifies the physical address of the request, can be specified address mode URI Template

  • method: method specifies the type of request, GET, POST, PUT, DELETE, etc.

  • consumes: submit a request specifying process of the content type (Content-Type), 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: used in front of the method parameters.

@RequestParam

@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 this anomaly encountered on the implementation of the following methods used in the above methods.

 
  

Guess you like

Origin blog.csdn.net/weixin_35681869/article/details/102714345