Detailed explanation of the difference between @Autowired and @Resource

1. @Autowired Configuration
First introduces the configuration of the @Autowired annotation, through which the dependency of the autowired Bean can be realized. For example, the following OrderService class instantiates and injects its dependent OrderRepository object through the @Autowired annotation:
@Service
public class OrderService {     @Autowired     private OrderRepository orderRepository;     // ... } It should be noted that by default, @ Autowired will perform dependency injection by type. If there are multiple beans of the same type in the container, an exception will be thrown. At this time, if you need to specify a specific Bean, you can use the @Qualifier annotation to achieve it.
    


    



@Service
public class OrderService {     @Autowired     @Qualifier("orderRepositoryImpl")     private OrderRepository orderRepository;     // ... } In addition, you can also use the @Autowired annotation on constructors, Setter methods, and other custom methods for dependency injection. @Service public class OrderService {     private OrderRepository orderRepository;     @Autowired     public OrderService(OrderRepository orderRepository) {         this.orderRepository = orderRepository;     }     /**      * Setter injection.      */     @Autowired     public void setOrderRepository(OrderRepository orderRepository ) {
    



    





    

    




    





        this.orderRepository = orderRepository;
    }
    
    /**
     * Custom method injection.
     */
    @Autowired
    public void injectOrderRepository(OrderRepository orderRepository) {         this.orderRepository = orderRepository;     }     // ... } 2. @Resource configuration Next, introduce @Resource The configuration of annotations can also realize the dependency of automatic assembly of beans. Compared to @Autowired, it uses names for dependency injection.


    



For example, the following OrderService class instantiates and injects its dependent OrderRepository object through the @Resource annotation:


@Service
public class OrderService {     @Resource(name = "orderRepositoryImpl")     private OrderRepository orderRepository;     // ... } It should be noted that by default, the bean name will be matched according to the property name. If you need to specify a name, you can use the name attribute to set it.
    


    


At the same time, @Resource also supports the type attribute, which can perform dependency injection according to the type. For example the following code:


@Service
public class OrderService {     @Resource(type = OrderRepository.class)     private OrderRepository orderRepository;     // ... } 3. Comparison of differences In summary, the difference between the two is mainly in the way of dependency injection.
    


    



For @Autowired, it performs dependency injection according to type. If there are multiple beans of the same type, you need to use @Qualifier to distinguish them. And @Resource performs dependency injection according to the name or type, and configures it through the name or type attribute.

In use, both can realize the dependency of autowiring beans, but the usage scenarios are slightly different. In general, if we only need to inject by type, then @Autowired is preferred. If you need to inject by name or type, @Resource is preferred.

It should be noted that in Spring 5.1 and above, @Autowired and @Qualifier have a new feature - Default Annotation Values. That is, when injecting by type, you can specify the specific Bean name through the default value of the @Qualifier annotation. In this way, the advantages of @Resource are weakened to a certain extent.
@Service
public class OrderService {     @Autowired     @Qualifier("orderRepositoryImpl")     private OrderRepository orderRepository;     // ... } Therefore, when using it, you should choose the appropriate annotation according to the specific situation to implement dependency injection.
    



    


Guess you like

Origin blog.csdn.net/weixin_65837469/article/details/130606424