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)
- @ResponseBody
Functions decorated with this annotation will populate the results directly into the HTTP response body, which is generally used to build restful APIs;
- @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).
- @RestController
The collection of @ResponseBody and @controller
- @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. The example code is as follows:
- @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.
- @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.
- @SpringBootApplication
Equivalent to @enableautoconfiguration, @ComponentScan, and @configuration collections.
- @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
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.
- @Qualifier:
When you have more than one bean of the same type, you can specify it with @qualifier ("name"). Use with @autowired
- @Resource (name= "name", type= "type"):
Without the bracketed content, the default byname. A similar thing to @autowired do.
- @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
- @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 parent class repository, 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:
Represents the property as the primary key.
- @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.
- @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.
- @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
- @Basic (Fetch=fetchtype.lazy):
tag to specify how entity properties are loaded
- @JsonIgnore:
The effect is that JSON serialization ignores some attributes in the Java Bean, and both serialization and deserialization are affected.
- @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.
- @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
- @RequestParam:
Used in front of the method's parameters.
@RequestParam String a =request.getparameter ("a").
- @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.
- Global exception Handling
@ControllerAdvice:
Contains @component. can be scanned to.
Handles exceptions uniformly.
- @ExceptionHandler (Exception.class):
Use the method above to indicate that you are encountering this exception by performing the following methods.
- @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