Explanation of Commonly Used Notes in springboot

  • 2021-07-18 07:54:01
  • OfStack

1: @ Qualifier

The @ Qualifier annotation specifies the name of the injected Bean so that ambiguity is cleared. Therefore, when @ Autowired and @ Qualifier are used together, the automatic injection strategy changes from byType to byName. Examples are as follows:

There is 1 interface:


public interface EmployeeService {
  public String getEmployeeById(Long id);
}

There are two implementation classes:


@Service("service")
public class EmployeeServiceImpl implements EmployeeService{
  @Override
  public String getEmployeeById(Long id) {
    return "0";
  }
}
@Service("service1")
public class EmployeeServiceImpl1 implements EmployeeService{
  @Override
  public String getEmployeeById(Long id) {
    return "1";
  }
}

The controller layer calls the service layer:


@RestController
public class EmployeeInfoControl {
  @Autowired
  @Qualifier("service")  // The value in parentheses is the implementation class @service Give the class a name when adding this annotation will not report an error 
  private EmployeeService employeeService;
 
  @RequestMapping("/emplayee.do")
  public void showEmplayeeInfo(){
    String employeeById = employeeService.getEmployeeById(1l);
    System.out.println("employeeById Value is "+employeeById);
  }
}
// An error will be reported at this time   Because @Autowired
//       private EmployeeService employeeService;
// What is injected is service Layer interface, there are two implementations at this time, and I don't know which implementation is bound. 
// At this time, it should be in @Autowired Cooperate below @Qualifier Annotation is used to explain which implementation class to bind to 
// As shown above 

2: @ RestController

The annotation is on the class, indicating that this is a control layer bean. Commonly used in front of the control layer class is a collection of @ ResponseBody and @ Controller.

@ ResponseBody: The function modified with this annotation will directly fill the result into the response body of HTTP, which is generally used to build api of RESTful and convert java object into json format data. @ Controller: Used to define the controller class. In the spring project, the controller is responsible for forwarding URL requests from users to the corresponding service interface (service layer).

3: @ RequestMapping

Provides routing information and is responsible for mapping specific functions in URL to Controller.

4: @ SpringBootApplication

This annotation is used on top of the running class and is equivalent to a collection of @ EnableAutoConfiguration, @ ComponentScan, and @ Configuration.

@ EnableAutoConfiguration: Spring Boot Auto Configuration (auto-configuration). @ ComponentScan: Means that the class is automatically discovered (scanned) and registered as Bean, which automatically collects all Spring components, including the @ Configuration class. We often use the @ ComponentScan annotation to search for beans and import it with the @ Autowired annotation. @ Configuration: It is equivalent to the traditional xml configuration file. If some third-party libraries need xml file, it is recommended to still use @ Configuration class as the main configuration class of the project-you can use @ ImportResource annotation to load xml configuration file.

5: @ Import

Used to import other configuration classes.

6: @ Autowired

Automatically import dependent bean.

7: @ Service

The annotation is on the class, indicating that this is a business layer bean.

8: @ Repository

The annotation is on the class, indicating that this is a data access layer bean. Use the @ Repository annotation to ensure that DAO or repositories provides exception translation, and the DAO or repositories classes modified by this annotation will be ComponetScan.

9: @ Query

Custom SQL query statement

10: @ Entity

Used before an entity class to indicate that this is an entity class.

11: @ Table (name= "")

Used before an entity class, 1 and @ Entity1, to indicate that the entity class maps a table in the database.

12: @ Column

Indicates that an attribute of an entity class is mapped to a field in a table, and contains the following settings: name: Database table field name; unique: Is it only 1; nullable: Can it be null; Length: Length.

13: @ Id

This annotation is used in entity classes and is written before which attribute indicates that the attribute is mapped to a field in the database as the primary key.

14: @ GeneratedValue

Represents the primary key generation strategy, and uses @ Id1

15: @ Transient

Indicates that the attribute is not a mapping to a field of a database table and is ignored by the ORM framework. If an attribute is not a field mapping for a database table, be sure to label it @ Transient.

Summarize


Related articles: