What is the difference between @Autowired and @Resource in Spring framework?


1 different sources

Insert image description here

@Autowired and @Resource come from different "parent classes", where @Autowired is an annotation defined by Spring, and @Resource is an annotation defined by Java, which comes from JSR-250 (Java 250 specification proposal).

Little knowledge: JSR is the abbreviation of Java Specification Requests, which means "Java specification proposal". Anyone can submit a JSR to the Java official, but only the finalized JSR will be released in the JSR-XXX format, such as JSR-250, and the released JSR can be regarded as a specification or standard of the Java language.

2. Dependence search order is different

The function of dependency injection is to first find the object in the Spring IoC container and then inject the object into the current class. There are two implementations of search: search by name (byName) or search by type (byType). Both @Autowired and @Resource use both name search and type search, but the order of search is different. Diametrically opposed.

2.1 @Autowired search order

@Autowired first searches based on type (byType). If there are multiple beans, it then searches based on name (byName) . Its specific search process is as follows:

Insert image description here

Regarding the above process, you can analyze it by viewing the org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessPropertyValues ​​implementation in the Spring source code. The source code execution process is shown in the figure below:

2.2 @Resource search order

@Resource is first searched based on the name. If it cannot be found (based on the name), then it is searched based on the type . Its specific process is as shown in the figure below:

Insert image description here

The above process can be analyzed in org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#postProcessPropertyValues ​​in the Spring source code. Although @Resource is defined by JSR-250, Spring provides a specific implementation. Its source code implementation is as follows:

2.3 Summary of search sequence

From the above analysis it can be concluded:

  • @Autowired First search based on type (byType), if there are multiple (Bean), then search based on name (byName);
  • @Resource first searches based on the name (byName), and if it cannot be found (based on the name), then searches based on the type (byType).

3. Supported parameters are different

Both @Autowired and @Resource can set parameters when using them, such as setting the name and type parameters for the @Resource annotation. The implementation code is as follows:

@Resource(name = "userinfo", type = UserInfo.class)
private UserInfo user;

However, the parameters and the number of parameters supported by the two are completely different. @Autowired only supports setting one required parameter, while @Resource supports 7 parameters . The supported parameters are as shown in the figure below:

Insert image description here

4. Different support for dependency injection

@Autowired and @Resource support different usages of dependency injection. Common dependency injection has the following three implementations:

  1. Property injection
  2. Constructor injection
  3. Setter injection

The implementation codes for these three implementation injections are as follows.

a) Attribute injection

@RestController
public class UserController {
    
    
    // 属性注入
    @Autowired
    private UserService userService;
 
    @RequestMapping("/add")
    public UserInfo add(String username, String password) {
    
    
        return userService.add(username, password);
    }
}

b) Constructor injection

@RestController
public class UserController {
    
    
    // 构造方法注入
    private UserService userService;
 
    @Autowired
    public UserController(UserService userService) {
    
    
        this.userService = userService;
    }
 
    @RequestMapping("/add")
    public UserInfo add(String username, String password) {
    
    
        return userService.add(username, password);
    }
}
复制代码

c) Setter injection

@RestController
public class UserController {
    
    
    // Setter 注入
    private UserService userService;
 
    @Autowired
    public void setUserService(UserService userService) {
    
    
        this.userService = userService;
    }
 
    @RequestMapping("/add")
    public UserInfo add(String username, String password) {
    
    
        return userService.add(username, password);
    }
}

Among them, @Autowired supports property injection, constructor injection and Setter injection, while @Resource only supports property injection and Setter injection. When using @Resource to implement constructor injection, the following error will be prompted:

Insert image description here

5. Compiler prompts are different

When using IDEA Professional Edition to write dependency injection code, if a Mapper object is injected, the @Autowired compiler will prompt an error message. The error content is as shown in the figure below:

Insert image description here

Although IDEA will display an error message, the program can be executed normally. Then, if change the dependency injection annotation to @Resource, the error message will not appear . The specific implementation is as follows:

6 Summary

@Autowired and @Resource are both annotations used to implement dependency injection (in Spring/Spring Boot projects), but they have 5 differences:

  1. The sources are different: @Autowired comes from the Spring framework, while @Resource comes from (Java) JSR-250;
  2. The order of dependency search is different: @Autowired first queries based on type and then based on name, while @Resource first queries based on name and then based on type;
  3. The supported parameters are different: @Autowired only supports setting 1 parameter, while @Resource supports setting 7 parameters;
  4. The usage of dependency injection supports different usage: @Autowired supports not only constructor injection, but also property injection and Setter injection, while @Resource only supports property injection and Setter injection;
  5. The prompts of the compiler IDEA are different: when injecting a Mapper object, the compiler will prompt an error using the @Autowired annotation, but will not prompt an error using the @Resource annotation.

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/132867054