Spring Boot must master 45 notes

16826084-52b89ab33ca2aeca.png

Point of attention, do not get lost; continually updated Java-related technologies and information architecture thermal paper! ! !

一.SpringBoot/spring

@SpringBootApplication:

Comprising @ Configuration, @ EnableAutoConfiguration, @ ComponentScan commonly used on the main class;

@Repository:

For annotation data access components, i.e. DAO components;

@Service:

Labeling assembly for business layer;

@RestController:

Labeling assembly for controlling layer (e.g., the action struts), and comprising @Controller @ResponseBody;

@Controller:

Control layer is used to label components, rather than with @Controller @RestController needed to return a page;

@Component:

It refers to the assembly, when the assembly is not good classification, we can use this annotation to mark;

@ResponseBody:

Indicates the result of the method returns directly written HTTP response body is generally used in the asynchronous data is acquired, @RequestMapping after use, the return value is typically resolved as the jump path,

After adding @ResponseBody returns the result as the jump path can not be interpreted, but directly written in the HTTP response body; json such as asynchronous data acquisition, after adding @ResponseBody, returns json data directly;

@RequestBody:

After this annotation parameters before adding that the parameters required. Acceptance json string to List and other objects;

@ComponentScan:

Component scans. Personal understanding is equivalent to, if the scan to class has @Component @Controller @Service such as these annotations, put these classes registered for the bean *;

@Configuration:

This class is indicated Bean configuration of an information source, corresponding to the XML, typically added to the main category;

@Bean:

It corresponds to the XML, in the above method, rather than a class, generating means of a bean, and to manage spring;

@EnableAutoConfiguration:

Let the Boot Spring to be automatically configured in accordance with application-dependent Spring Framework stated, typically added to the main category;

@AutoWired:

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 there are a plurality of the same type of Bean, can be specified @Qualifier ( "name"). Used in conjunction with @Autowired;

@Resource(name=”name”,type=”type”):

There is no content within the parentheses, the default byName. @Autowired doing similar things with;

@RequestMapping:

RequestMapping is a process for annotation request address mapping, it can be used for class or method. For the class, in response to request by any methods that are based on the class as a parent path address;

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) type request comprises a request to return the specified type only.

@ GetMapping, @ PostMapping such as:

Corresponds @RequestMapping (value = "/", method = RequestMethod.GetPostPutDelete etc.). Is a combination of notes;

@RequestParam:

Method parameters used on the front. Equivalent request.getParameter;

@PathVariable:

Path variable. The RequestMapping ( "user / get / mac / {macAddress}");

public String getByMacAddress(
@PathVariable(“macAddress”) String macAddress){
    //do something;
}

Parameters and braces of the same name, then the notes in brackets content can not fill.

Two .Jpa

@Entity:

@Table(name=”“):

That this is an entity class. JPA generally used, the use of both a general comment, but if the table name and the name of the same class, then the entity, @ Table may be omitted;

@MappedSuperClass:

It is used in the determination of the parent entity. Properties subclass can inherit the parent class;

@NoRepositoryBean:

Usually used as the parent class repository, this annotation, spring not to instantiate the repository;

@Column:

If the field name and column names the same, it can be omitted;

@Id:

Indicates that the primary key attribute;

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = “repair_seq”):

Represents the 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 database field mapping table, be sure to mark it as @Transient, otherwise, ORM framework annotated as its default @Basic;

@Basic(fetch=FetchType.LAZY):

Tag to specify the attributes of an entity loading;

@JsonIgnore:

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.

III. Global Exception Handling

@ControllerAdvice:

It contains @Component. It can be scanned. Unified handle exceptions;

@ExceptionHandler(Exception.class):

In the above method represented by this anomaly encountered on the implementation of the following methods.

Four .springcloud

@EnableEurekaServer:

Used in springboot startup class, said it was a eureka service registry;

@EnableDiscoveryClient:

Used in springboot startup class, that this is a service that can be found registry;

@LoadBalanced:

Open load balancing;

@EnableCircuitBreaker:

Used in the startup class, turn the circuit breaker function;

@HystrixCommand(fallbackMethod=”backMethod”):

Used in the method, fallbackMethod designated breaking callback method;

@EnableConfigServer:

Used in the startup class, he said it was a distribution center, open Config Server;

@EnableZuulProxy:

Open zuul route, used in the startup class;

@SpringCloudApplication:

contain

  • @SpringBootApplication
  • @EnableDiscovertyClient
  • @EnableCircuitBreaker

SpringBoot notes are registered service center Eureka notes, annotations breaker. For SpringCloud, this is due every three micro-services must comment, so it launched a @SpringCloudApplication this collection of notes.

Written in the last

Point of attention, do not get lost; continually updated Java-related technologies and information architecture thermal paper! ! !

Reproduced in: https: //www.jianshu.com/p/f457a575e155

Guess you like

Origin blog.csdn.net/weixin_33851429/article/details/91072136
Recommended