Spring Boot Annotations Daquan

Source: Internet
Author: User

[Springboot series]--springboot Annotation Daquan

I. List of annotations (annotations)
@SpringBootApplication: Contains @componentscan, @Configuration, and @enableautoconfiguration annotations. Where @componentscan let spring boot scan to the configuration class and add it to the program context.

@Configuration an XML configuration file that is equivalent to spring and can be checked for type safety using Java code.

@EnableAutoConfiguration automatic configuration.

@ComponentScan component scans to automatically discover and assemble some beans.

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

@RestController annotations are collections of @controller and @responsebody, which means that this is a controller bean, and that it is a rest-style controller that fills the return value of the function directly into the HTTP response body.

@Autowired Auto-Import.

@PathVariable get parameters.

@JsonBackReference Resolve nested outer chain issues.

@RepositoryRestResourcepublic used in conjunction with Spring-boot-starter-data-rest.

Second, the annotation (annotations) detailed

@SpringBootApplication: Assert that spring boot automatically configures the program as necessary, which is equivalent to: @Configuration, @EnableAutoConfiguration and @ Componentscan of 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 return result of the method is written directly to the HTTP response body, which is generally used when fetching data asynchronously for building restful APIs. After using @requestmapping, the return value usually resolves to the jump path, plus @responsebody returns the result will not be resolved to the jump path, but is written directly to the HTTP response body. For example, asynchronously fetching JSON data, plus @responsebody, will return the JSON data directly. This annotation is generally used in conjunction with @requestmapping. Example code:

@RequestMapping ("/test") @ResponseBody public String test () {return "OK";}

@Controller: Used to define the controller class, which is the responsibility of the controller in the spring project to forward URL requests from the user to the corresponding service interface (services layer), generally this annotation is in the class, usually the method needs to match the annotation @requestmapping. Example 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");   The rendering is displayed using Hello.html or HELLO.FTL templates.   return "/hello";}}

@RestController: Used to label control-layer components, such as action in Struts, @ResponseBody and @controller collections. Example 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 that is responsible for mapping the URL to a specific function in the controller.

@EnableAutoConfiguration: Spring Boot automatic configuration (auto-configuration): Try to automatically configure your spring app based on the jar dependencies you add. For example, if HSQLDB is present under your classpath and you do not manually configure any database connection beans, then we will automatically configure a memory-type (in-memory) database. You can add @enableautoconfiguration or @springbootapplication annotations to a @configuration class to select automatic configuration. If you find that you have applied specific auto-configuration classes that you do not want, you can disable them by using the exclusion properties of the @enableautoconfiguration annotations.

@ComponentScan: Indicates that the class is automatically discovered by the scan component. As a personal understanding, if you scan a class with annotations such as @component, @Controller, @Service, and register as a bean, you can automatically collect all of the spring components, including the @configuration class. We often use @componentscan annotations to search for beans and to import them in conjunction with @autowired annotations. All spring components, including the @configuration class, can be automatically collected. We often use @componentscan annotations to search for beans and to import them in conjunction with @autowired annotations. If it is not configured, Spring boot scans the class that uses @service, @Repository, and so on, under the package under the Startup class and under the sub-package.

@Configuration: Equivalent to a traditional XML configuration file, if some third-party libraries need to use an XML file, it is recommended that the @configuration class be used as the configuration main class for the project-you can use @importresource annotations to load the XML configuration file.

@Import: Used to import additional configuration classes.

@ImportResource: Used to load an XML configuration file.

@Autowired: Automatic import of dependent beans

@Service: Components typically used to decorate the Service layer

@Repository: Use @repository annotations to ensure that DAO or repositories provides exception translation, and that this annotation-decorated DAO or repositories class is Componetscan discovered and configured, There is also no need to provide them with XML configuration items.

@Bean: The @bean annotation method is equivalent to the Bean configured in XML.

@Value: The value of the property injected into the spring boot application.properties configuration. Example code:

@Value (Value = "#{message}") private String message;

@Inject: Equivalent to the default @autowired, except that there is no required attribute;

@Component: Refer to the component, we can use this annotation to annotate when the component is not well categorized.

@Bean: the equivalent of XML, placed above the method, rather than the class, meaning to produce a Bean and give it to spring management.

@AutoWired: Automatic import of dependent beans. Bytype Way. The configuration of the bean to use, the completion of the properties, methods of assembly, it can be the class member variables, methods and constructors to label, complete the work of automatic assembly. When added (Required=false), even if the bean can not find the error.

@Qualifier: When you have more than one bean of the same type, you can specify it with @qualifier ("name"). Used in conjunction with the @autowired. @Qualifier qualified descriptors can be injected in addition to the name, but finer-grained control over how to select candidates is used in the following ways:

@Autowired @Qualifier (value = "Demoinfoservice") private Demoinfoservice Demoinfoservice;

@Resource (name= "name", type= "type"): The default byname if there is no bracketed content. A similar thing to @autowired do.

Third, JPA annotations

@Entity: @Table (name= ""): Indicates that this is an entity class. Typically used for JPA these two annotations are generally used, but if the table name and entity class name are the same, @Table can omit

@MappedSuperClass: Used to determine the entity that is the parent class. The attribute subclasses of the parent class can inherit.

@NoRepositoryBean: Typically used as a repository of the parent class, with this annotation, spring does not instantiate the repository.

@Column: If the field name is the same as the column name, you can omit it.

@Id: Indicates that the property is a primary key.

@GeneratedValue (strategy = Generationtype.sequence,generator = "Repair_seq"): Indicates that the primary key generation policy is SEQUENCE (can be auto, IDENTITY, Native, auto means that it can switch between multiple databases, and specifies that sequence's name is REPAIR_SEQ.

@SequenceGeneretor (name = "Repair_seq", Sequencename = "Seq_repair", allocationsize = 1): Name is a sequence of names for use, Sequencename is the sequence name of the database, two names can be the same.

@Transient: Indicates that the property is not a mapping to a field in a database table, and the ORM framework ignores the property. If a property is not a field mapping for a database table, it must be marked as @transient, otherwise the ORM framework defaults to its annotation as @basic. @Basic (Fetch=fetchtype.lazy): tag to specify how entity properties are loaded

@JsonIgnore: The effect is that JSON serialization ignores some properties in the Java Bean, and both serialization and deserialization are affected.

@JoinColumn (name= "LoginId"): one-to-one: a foreign key in this table that points to another table. One-to-many: the other table points to the foreign key of the table.

@OneToOne, @OneToMany, @ManyToOne: A one-to-many, multiple-to-a in the Hibernate configuration file.

Iv. SPRINGMVC Related annotations

@RequestMapping: @RequestMapping ("/path") indicates that the controller handles all ur l requests for "/path". Requestmapping is an annotation that handles request address mappings and can be used on classes or methods.
On a class, the method that represents all response requests in a class is the parent path of the address. The note has six properties:
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Value: Specifies the actual address of the request, the specified address can be URI Template mode
Method: Specifies the type of method requested, GET, POST, PUT, delete, etc.
Consumes: Specifies the type of submission to process the request (Content-type), such as application/json,text/html;
Produces: Specifies the type of content returned, only if the type in the request header (Accept) contains the specified type to return

@RequestParam: Used in front of the parameters of the method.
@RequestParam
String a =request.getparameter ("a").

@PathVariable: Path variable. Such as

The arguments are the same as the names in the curly braces.

V. Global exception Handling

@ControllerAdvice: Contains @component. can be scanned to. Handles exceptions uniformly.

@ExceptionHandler (Exception.class): Use the method above to indicate that you have encountered this exception to perform the following methods.

Spring Boot Annotations Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.