The difference between @Resource and @Autowire annotations in Spring


1. The difference between @Resource and @Autowire annotations

Both @Resource and @Autowired are annotations used for dependency injection, but they have some differences.

@Autowired is an annotation in the Spring framework, which can be used to mark fields, constructors, methods, etc., indicating the need for automatic assembly. It can be used to inject dependent beans. An exception may be thrown if more than one bean meets the criteria.

@Resource is an annotation that comes with Java. It can be used to mark fields, methods, etc., indicating that automatic assembly is required. It can be used to inject dependent beans. If multiple beans meet the criteria, they will be matched by name.

Summarize:

  • @Autowired is an annotation in the Spring framework, which is used to mark beans that need to be automatically assembled.
  • @Resource is an annotation that comes with Java and is used to mark beans that need to be automatically assembled.
  • @Autowired is assembled by type. If there are multiple beans of the same type, an exception will be thrown; @Resource is assembled by name. If the name does not exist, type assembly will be used.

2. Detailed description

The following is an example of dependency injection using @Autowired and @Resource annotations:
the code is as follows (example):

// 定义一个服务类
@Service
public class MyService {
    
    
    // 使用@Autowired注入一个Dao
    @Autowired
    private MyDao myDao;
}

// 定义一个Dao类
@Repository
public class MyDao {
    
    
    // 使用@Resource注入一个DataSource
    @Resource
    private DataSource dataSource;
}

Using the @Autowired annotation, it will be assembled by type, and if there are multiple beans of the same type, an exception will be thrown.
With the @Resource annotation, it will be assembled by name, and if the name does not exist, it will be assembled by type.

  • If there are not multiple beans of the same type in your project, then @Autowired and @Resource can be used interchangeably. If there are multiple beans of the same type, then @Resource must be used for specified name injection.
  • If there are multiple beans of the same type, but you do not use @Resource to specify the name, an exception will be thrown, so when using the @Autowired annotation, make sure there is only one bean of the same type.

What is the same type of Bean

In the above example, the same type of bean means that there are multiple beans of type MyDao in the Spring container. If the @Autowired annotation is used for injection, the Spring container does not know which MyDao type bean should be injected. Hence an exception is thrown.

In other words, if you have multiple classes in the project that implement the same interface or inherit the same class, and these classes are marked as @Service, @Repository, etc., and you use this in other places If the interface or class is injected, then there will be multiple beans of the same type.

For example, if you have two classes A and B that implement interface I, and both are marked as @Service, then when you use I for injection in other places, there will be multiple beans of the same type Condition.


Summarize

In general, both @Autowired and @Resource are annotations used for dependency injection, but they have some differences:

  • @Autowired is an annotation in the Spring framework. It is used to mark beans that need to be automatically assembled. By default, they are assembled by type. If there are multiple beans of the same type, an exception will be thrown.
  • @Resource is an annotation that comes with Java. It is used to mark beans that need to be automatically assembled and assembled according to the name. If the name does not exist, the type assembly will be used.

If there are not multiple beans of the same type in your project, then @Autowired and @Resource can be used interchangeably. If there are multiple beans of the same type, then @Resource must be used for specified name injection.

Tip: For more content, please visit Clang's Blog: https://www.clang.asia

Guess you like

Origin blog.csdn.net/u012899618/article/details/128718839