Ali architects take you explain SpringBoot notes with an article

First, notes (annotations) list
@SpringBootApplication: contains @ ComponentScan, @ Configuration and @EnableAutoConfiguration comment. Which @ComponentScan let Spring Boot Configuration class to scan and add it to the program context.

@Configuration equivalent spring XML configuration file; using java code type safety check.

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

Annotations are @ResponseBody @RestController @Controller 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 outside the chain of nested solve the problem.

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

Second, notes (annotations) Detailed
@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: 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: 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");

// use hello.html or hello.ftl template rendered display.

return"/hello";

}

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

package com.kfit.demo.web;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

*

  • @author Angel (QQ exchange group: 193341332, QQ: 412887952)

  • @version v.0.1

  • @date 7:26:04 2016 Nian 7 afternoon of May 29

*/

@RestController

@RequestMapping(“/demoInfo2”)

publicclass DemoController2 {

@RequestMapping("/test")

public String test(){

return"ok";

}

@RequestMapping: providing road information, URL mapping Controller is responsible for the specific function.

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

@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: 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", 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: 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 serialization java bean in some of the properties ignored, 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 annotations
@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: 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: 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.

Like the author of this article can give a point like, look, will share Java-related articles every day! There are benefits presented from time to time, including the consolidation of learning materials, interview questions, such as source code ~ ~

Guess you like

Origin blog.51cto.com/14440597/2423628