Spring Boot Encyclopedia notes, is really too full!

First, notes (annotations) list 

 

@S pringBootApplication : it 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.

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 : means return the process results 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 : 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 . 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 control layer label assembly (e.g., the action struts), @ ResponseBody @Controller and the collection. Recommended: the Spring MVC common annotations, you will a few?

 

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

@EnableAutoConfiguration : the Boot Spring 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. Recommended: the Spring core of the Boot 3 Detailed notes .

@Import : used to introduce other configuration class. Recommended: the Spring core 25 the Boot comment!

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

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

 

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

@ The Component : Refers to the component when the component is not classified, 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:

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

 

@Resource (name = "name", of the type = "of the 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 : 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.

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

@Id : indicates that the primary key attribute.

@GeneratedValue (= GenerationType.SEQUENCE Strategy, 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 name sequence in order 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 model Method : Method specifies the type of request, GET, POST, PUT, DELETE, etc. 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 a = request.getParameter ( "a ").

@PathVariable : path variable. Such as

 


 
RequestMapping(“user/get/mac/{macAddress}”)

public String getByMacAddress(@PathVariable String macAddress){

//do something;

}

 

 

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.

 

 

Guess you like

Origin www.cnblogs.com/qwlscn/p/11495378.html