Annotations used in spring/spring boot/spring MVC

Source: Internet
Author: User

XML configuration files are almost completely deprecated in spring boot, and the topic of this article is to analyze commonly used annotations.

Spring was initially designed to address the intrusion of large enterprise frameworks such as EJBS into applications, so relying heavily on configuration files for "non-intrusive" pojo added functionality, but starting with spring 3.x, one of the most popular things that spring has been criticized for is the configuration of hell. A variety of XML files, the problem is very difficult to troubleshoot. Starting with Spring 4.x, Spring.io offers three ways to weave beans:

    • Use annotations: Implicit configuration, such as: @Autowired, @Bean, @Component, etc., to simplify the XML file with annotations.
    • Using Java files: Display configuration, which has the advantage over XML configuration, is type-safe
    • Take advantage of traditional XML configuration files
List of annotations (annotations)
    1. @ResponseBody
      Functions decorated with this annotation will populate the results directly into the HTTP response body, which is generally used to build restful APIs;
    2. @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).
    3. @RestController
      The collection of @ResponseBody and @controller
    4. @RequestMapping
      Provides routing information that is responsible for mapping the URL to a specific function in the controller.
    5. @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. The example code is as follows:
    6. @ComponentScan
      Indicates that the class is automatically discovered (scanned) and registered as a bean, and all spring components, including the @configuration class, are automatically collected. We often use @componentscan annotations to search for beans and to import them in conjunction with @autowired annotations.
    7. @Configuration
      Equivalent to a traditional XML configuration file, if some third-party libraries need to use an XML file, it is recommended that you still use the @configuration class as the configuration main class for your project-you can load an XML configuration file with @importresource annotations.
    8. @SpringBootApplication
      Equivalent to @enableautoconfiguration, @ComponentScan, and @configuration collections.
    9. @Import
      Used to import additional configuration classes.
    10. @ImportResource
      Used to load an XML configuration file.
    11. @Autowired
      Automatic import of dependent beans
    12. @Service
      Components typically used to decorate the service layer
    13. @Repository
      Using @repository annotations ensures that DAO or repositories provides exception translation, and that the annotation-decorated DAO or repositories class is Componetscan discovered and configured without the need to provide them with an XML configuration item.
    14. @Qualifier:
      When you have more than one bean of the same type, you can specify it with @qualifier ("name"). Use with @autowired
    15. @Resource (name= "name", type= "type"):
      Without the bracketed content, the default byname. A similar thing to @autowired do.
    16. @Entity:
      @Table (name= ""):
      Indicates that this is an entity class. Typically used for JPA
      These two annotations are used in a common piece, but if the table name and entity class name are the same, @Table can omit the
    17. @MappedSuperClass:
      Used to determine the entity that is the parent class. The attribute subclasses of the parent class can inherit.
    18. @NoRepositoryBean:
      Typically used as a parent class repository, with this annotation, spring does not instantiate the repository.
    19. @Column:
      If the field name is the same as the column name, you can omit it.
    20. @Id:
      Represents the property as the primary key.
    21. @GeneratedValue (strategy = Generationtype.sequence,generator = "Repair_seq"):
      Indicates that the primary key generation policy is sequence (can be auto, IDENTITY, native, and so on, auto means that it can be used between multiple databases
      switch), specify that sequence's name is REPAIR_SEQ.
    22. @SequenceGeneretor (name = "Repair_seq", Sequencename = "Seq_repair", allocationsize = 1)
      Name is sequence for use, Sequencename is the sequence name for the database, and two names can be consistent.
    23. @Transient:
      Indicates that the property is not a mapping to a field in a database table, which the ORM framework ignores.
      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
    24. @Basic (Fetch=fetchtype.lazy):
      tag to specify how entity properties are loaded
    25. @JsonIgnore:
      The effect is that JSON serialization ignores some attributes in the Java Bean, and both serialization and deserialization are affected.
    26. @JoinColumn (name= "LoginId"):
      One-to-one: the 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
      Corresponds to a one-to-many, multi-pair in the Hibernate configuration file.
    27. @RequestMapping:
      Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. Used on classes, representing classes in
      All responses to a request are made with that address as the parent path.
      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
    28. @RequestParam:
      Used in front of the method's parameters.
      @RequestParam String a =request.getparameter ("a").
    29. @PathVariable:
      The PATH variable.
      such as requestmapping ("user/get/mac/{macaddress}")
      public string getbymacaddress (@PathVariable string macAddress) {
      Do something;
      }
      The arguments are the same as the names in the curly braces.
    30. Global exception Handling
      @ControllerAdvice:
      Contains @component. can be scanned to.
      Handles exceptions uniformly.
    31. @ExceptionHandler (Exception.class):
      Use the method above to indicate that you are encountering this exception by performing the following methods.
    32. @Bean:
      Equivalent to the <BEAN></BEAN> in XML, placed above the method rather than the class, meaning to produce a bean and give it to spring
      Management.

Annotations used in spring/spring boot/spring MVC

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.