[SSM-Spring Chapter 03] Main annotations in Spring


One of the core functions of Spring is IOC, which is to initialize Beans into the container. Beans can be loaded into the container using Spring annotation or Spring XML configuration . (Use annotations to load some classes in java into the container)

Component class annotation @Component, @Repository, @Service, @Controller

The functions of @Repository, @Service, and @Controller are the same as @Component, but in order to make the purpose of the annotation class clearer (hierarchical), it is recommended to use in actual development:
@Repository annotation data access layer (DAO layer), use @Service Annotate the business logic layer (Service layer) and use @Controller to annotate the controller layer (control layer).

1. @Component is annotated as a common spring Bean class.
  This annotation is a generalized concept, which only represents a component object (Bean), which can be applied at any level.
2. @Repository is annotated as a component class of
  the DAO layer. This annotation is used to identify the data access layer (DAO) class as Bean, that is, annotated data access layer Bean, and its function is the same as @Component().
3. @Service is annotated as a component class of
  the Service layer (business logic layer). This annotation is used to annotate a business logic component class (Service layer), and its function is the same as @Component().
4. @Controller Annotates a controller component class.
  This annotation is used to annotate a controller component class (Spring MVC Controller), and its function is the same as @Component().

Common annotations @Autowired, @Resource when assembling beans

@Autowired

  This annotation can annotate class member variables, methods and construction methods to complete the work of automatic assembly ( assembly according to the type of Bean). Eliminate setter and getter methods through the use of @Autowired. The default is to assemble according to the type of Bean. (Belonging to Spring's org.springframework.beans.factory.annotation package)

@Resource (not belonging to spring annotation, it is javax.annotation annotation)

  This annotation has the same function as @Autowired . The difference is that the annotation is assembled and injected according to the name by default. Only when no Bean matching the name is found, the injection will be assembled according to the type ; while @Autowired is assembled according to the type of Bean by default, if you want to assemble and inject according to the name , You need to use it in conjunction with the @Qualifier annotation.
  @Resource annotation has two attributes: name and type. The name attribute specifies the name of the Bean instance, that is, to assemble and inject according to the name; the type attribute specifies the Bean type, that is, to assemble according to the type of Bean

@Qualifier

   This annotation is used in conjunction with the @Autowired annotation. When the @Autowired annotation needs to be assembled and injected according to the name, it needs to be used in conjunction with the annotation. The instance name of the Bean is specified by the parameter of the @Qualifier annotation.

Precautions

   The @Autowired annotation is assembled by type by default. If the container contains multiple beans of the same type, an exception that the specified type of bean cannot be found will be reported when the container is started. The solution is to combine the @Qualifier annotation to qualify and specify the injected bean name.

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108682589