Learning - Common Annotations

Common Annotations

@SpringBootApplication
@SpringBootApplication is a complex annotations, including @ComponentScan, and @ SpringBootConfiguration, @ EnableAutoConfiguration

  • @SpringBootConfiguration inherited from @Configuration, consistent both functions are denoted by the current class is the class configuration, and the current will be within a class declaration of example one or more annotation markers @Bean method srping into the container, and the instance name is the name of the method
  • The role of @EnableAutoConfiguration start automatically configured.
  • @EnableAutoConfiguration annotation means that Springboot to configure the default configuration of your project according to the package you add a jar, for example based on spring-boot-starter-web, to determine whether you need to add a project webmvc and tomcat, will automatically help you Configuring web projects needed in the default configuration.
  • @ComponentScan, scans the current sub-packet and the packet is marked annotated @ Component, @ Controller, @ Service, @ Repository class and into the spring receptacle for management.

@EnableJpaRepositories
@EnableJpaRepositories Srping JPA Annotations for code configuration for substituted xml configuration file in the form of

@EnableAspectJAutoProxy
indicates turning AOP proxy auto-configuration, if represented with @EnableAspectJAutoProxy cglib generated using proxy object; provided @EnableAspectJAutoProxy (exposeProxy = true) indicates that the subject is exposed through a proxy aop frame, aopContext access to

@EnableScheduling & @Scheduled
regular tasks add @EnableScheduling open support for regular tasks on a configuration class, add a scheduled task @Scheduled statement to be performed on the appropriate method.
Scheduled annotation which has the following parameters:

  • cron;
  • zone;
  • fixedDelay and fixedDelayString;
  • fixedRate and fixedRateString;
  • initialDelay和initialDelayString
  1. cron is an expression to set the timing of the execution, such as 0 0/5 * * *? executed once every five minutes
  2. zone represents a time zone execution time
  3. fixedDelay and fixedDelayString represents a fixed delay time to perform, after a task is completed, the implementation of how long the delay
  4. fixedRate and fixedRateString represent a fixed-frequency execution, after the task began, how long after you begin
  5. initialDelay and initialDelayString represents an initial delay time, the delay before the first time is called

@ConditionalOnProperty
control a configuration is in effect. Specific operation is achieved by its name and attributes two havingValue, wherein an attribute name used to read the value from the application.properties, if the value is null, false is returned; if the value is not null, then the havingValue value compared to the value specified, as if it returns true; otherwise false. If the return value is false, the configuration does not take effect; true to take effect.

@PathVariable
@PathVariable bind URI Template variable value
@PathVariable request is used to obtain the dynamic parameters of the url
parameter @PathVariable template variable for the URL request is mapped to the function of the processing method

@Repository
for annotation data access components, i.e. DAO components.

@Service
for labeling business layer components.

@RestController
for labeling component control layer (e.g., the action struts), and comprising @Controller @ResponseBody

@Controller
for labeling control layer components, if necessary with a return to the page instead @RestController @Controller

@Component
refers to the components when the components are classified bad, we can use this annotation to mark.

@ResponseBody
represents returns the result of the process of directly writing HTTP response body.
Generally used in the asynchronous data is acquired, @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, will return json data directly

@RequestBody
ago parameters after adding the comment that the parameters required. Acceptance json string to List and other objects.

@ComponentScan
component scans. Personal understanding is equivalent to, if the scan to have these annotated classes @Component, @ Controller, @ Service and so on, put these classes registered as a bean

@Configuration
noted Bean class is configured information source, corresponding to the XML, typically added to the main class.

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

@EnableAutoConfiguration
allows to automatically configure the Boot Spring Spring framework according to the stated application-dependent, is generally added to the main class

@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") is not within the braces, the default byName. Doing similar things with @Autowired

@RequestMapping
@RequestMapping annotation is used to process a 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

@ GetMapping, @ PostMapping and other
equivalent @RequestMapping (value = "/", method = RequestMethod.Get \ Post \ Put \ Delete , etc.). It is a combination comment

@RequestParam
used in front of the method parameters. Equivalent request.getParameter ().

@PathVariable:
path variable. Such as

    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


Jpa related

@Entity:
@Table (name = "")

indicates that this is an entity class. JPA generally used;
these two notes generally one used, 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, may be omitted

@Id
indicates that the primary key attribute

@GeneratedValue (strategy = GenerationType.SEQUENCE, generator =
"repair_seq") represents the primary key generation strategy is a 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 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 property.
If the property is not a field mapping database tables, be sure to mark it as @Transient, otherwise, ORM framework of its default as @ Basic

@Basic (fetch = FetchType.LAZY)
tag to specify the entity properties 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
the @ManyToOne

correspond Hibernate configuration file, one to one, one to many, many to one


Global exception handler associated

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

@ExceptionHandler (Exception.class)
expressed this anomaly encountered on the implementation of the following methods used in the above method

springcloud related

@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, open 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 routing, used on startup class

@SpringCloudApplication
contain @ SpringBootApplication, @ EnableDiscoveryClient, @ EnableCircuitBreaker three notes

Guess you like

Origin www.cnblogs.com/zd-blogs/p/12015906.html