SpringBoot: The difference between Autowired and Resource keywords

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

SpringBoot: The difference between Autowired and Resource keywords


foreword

Many friends use the Autowired and Resource keywords to inject beans when developing, but few people pay attention to the difference between the two.
This article details the similarities and differences between the Autowired and Resource keywords.


1. Similarities

Both @Resource and @Autowired are used for bean injection, and in fact @Resource is not a Spring annotation. Its package is javax.annotation.Resource, which needs to be imported, but Spring supports the injection of this annotation.

Two, the difference

1.@Autowired

The annotations provided by @Autowired for Spring need to import the package org.springframework.beans.factory.annotation.Autowired;
only inject byType.

public class TestServiceImpl {
    
    
	 // 下面两种@Autowired只要使用一种即可
	 @Autowired
	 private UserDao userDao; // 用于字段上
	 
	 @Autowired
	 public void setUserDao(UserDao userDao) {
    
     // 用于属性的方法上
	 this.userDao = userDao;
	 }
}

The @Autowired annotation assembles dependent objects according to type (byType). By default, it requires that dependent objects must exist. If null values ​​are allowed, you can set its required attribute to false. If we want to assemble by name, we can use it in conjunction with the @Qualifier annotation.

public class TestServiceImpl {
    
    
	 @Autowired
	 @Qualifier("userDao")
	 private UserDao userDao;
	}
}

2.@Resource

@Resource is automatically injected by ByName by default, provided by J2EE, and the package javax.annotation.Resource needs to be imported.

@Resource has two important attributes: name and type, and Spring resolves the name attribute of the @Resource annotation to the name of the bean, and the type attribute resolves to the type of the bean. Therefore, if the name attribute is used, the byName automatic injection strategy is used, and when the type attribute is used, the byType automatic injection strategy is used. If neither the name nor the type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.

public class TestServiceImpl {
    
    
	 // 下面两种@Resource只要使用一种即可
	 @Resource(name="userDao")
	 private UserDao userDao; // 用于字段上
	 
	 @Resource(name="userDao")
	 public void setUserDao(UserDao userDao) {
    
     // 用于属性的setter方法上
	 this.userDao = userDao;
	 }
}

Note: It is best to put @Resource on the setter method, because this is more in line with the object-oriented thinking, and the properties are manipulated through set and get instead of directly.

@Resource assembly order:

① If both name and type are specified, the only matching bean is found from the Spring context for assembly, and an exception is thrown if it cannot be found.

② If the name is specified, the bean with the matching name (id) will be searched from the context for assembly, and an exception will be thrown if it cannot be found.

③If type is specified, find the only bean that matches similarly from the context for assembly, if it cannot find or find more than one, an exception will be thrown.

④If neither name nor type is specified, it will be automatically assembled according to the byName method; if there is no match, it will fall back to an original type for matching, and if it matches, it will be automatically assembled.

The role of @Resource is equivalent to @Autowired, except that @Autowired is automatically injected according to byType.

Guess you like

Origin blog.csdn.net/qq_46119575/article/details/129700776