The difference between @Autowired and @Resource

Install mInsta

1 scene

When spring is in progress 声明式依赖注入, the annotations that are often used are @Autowiredand @Resource. This article mainly records the difference between the two in use.

2 difference

2.1 Definition

@AutowiredIt is an annotation of spring and an annotation @Resourceof the JDK JSR-250 specification. Spring supports this annotation.

When looking for the injection of the corresponding bean, @Autowiredthe principle is ” 类型优先” and @Resourcethe principle is ” name优先“.

The main differences between the two are as follows (detailed analysis will be carried out later):

2.1.1 @Autowired type priority

(1) Matching content

the type of bean , as 注入属性的类型follows: .
The name of the bean is: @Qualifier指定名称(优先级高)>注入属性的名称(默认)

The matching priority is: first 类型find the bean according to the injected bean.

  • Through the type of bean, if found, the 唯一的beaninjection is successful;
  • If no bean of this type is found, an error will be reported;
  • Find multiple beans of the same type, and then find them by bean name. If the bean is found, it will return, if it is not found, it will report an error.

(2) Processing of beans without corresponding types

If @Autowired(required = false) is set, the bean type does not exist, and no exception will be thrown; otherwise, if a bean of this type exists, an exception will be thrown.

Note that the required = false here is valid only when the bean type does not exist. If there is a bean type, but the name cannot be matched, an exception will still be thrown.

2.1.2 @Resource’s name takes precedence

(1) Matching content

The type of bean is: 注入属性的类型(@Resource can also be specified with a type parameter, and the priority is high).
the bean name is: @Resource指定的name(优先级最高)> @Qualifier指定名称(优先级次高)>注入属性的名称(默认)

The matching priority is: first find the bean based on the bean’s name.

  • Through name, find 唯一的beanand "bean的类型"sum "注入属性的类型"一致, the injection is successful
  • If the name is passed, no unique bean can be found. If the name is not @Resource注解参数namespecified (if the name is specified by @Resource or @Qualifier, an error is reported), but the name of the bean is specified by the name of the attribute, it will try to find the bean based on 注入属性的类型+ 属性的name, and the subsequent judgment logic is the same as @Autowired , which is as follows :
    • Through the type of bean, if found, the 唯一的beaninjection is successful;
    • If not found 此类型的bean, an error will be reported;
    • Find multiple beans of the same type, and then find them by bean name. If the bean is found, it will return, if it is not found, it will report an error.

(2) Processing of beans without corresponding types

Throw an exception, report an error

2.2 Range of use

(1) General scope

Both can be used on 成员属性and setter方法.

(2) Description of construction method injection

Which @Autowiredcan also be used in 构造方法the start Spring4.3, if bean define only a constructor, do not require the re-constructor @Autowired annotation, also automatically injected.
Java executes the construction method first, and then injects dependency properties through annotations. 优先于Declarative injection of member properties of bean on the construction method and declarative injection on the setter method.

Injecting properties on the constructor can be restricted bean的注入顺序, and the order of the parameters of the constructor is the order in which the beans are injected.

Spring recommends:

Always use the constructor based on dependency injection in the bean. Always use assertions for mandatory dependencies.

The following code:

private OtherService otherService;
@Autowired
public void MyComponent(OtherService otherService) {
     Assert.assertNotNull(otherService);
    this.otherService = otherService;
}

3 @Autowired annotation

3.1 Associated annotations

The annotation that can be used together is org.springframework.beans.factory.annotation.Qualifier , which is mainly used to specify beans name.

code show as below:

@Qualifier(value = "userService3")
@Autowired
private UserService userService;

The name ” userService3 ” of the bean specified by @Qualifier has a higher priority than the name ” userService ” corresponding to the variable name .

3.2 Matching principle

The main matching principle is 类型优先that the relevant solutions are: @Autowired , @Qualifier .

3.3 Sample code

//---------------------
@Autowired
private OtherService otherService;
//---------------------
@Qualifier(value = "userServiceImplA")
@Autowired
private UserService userService;
//---------------------
@Autowired(required = false)
private UserService userServiceImplA;
//---------------------
@Autowired
private UserService userServiceImplA;
@Autowired
private UserService userServiceImplB;

4 @Resource annotation

4.1 Matching principle

The main matching principle is 名称优先that the relevant solutions are: @Resource , @Qualifier (do not specify the name in @Resource, you can also specify the name in @Qualifier, the name in @Resource has higher priority).

4.2 Sample code

//---------------------
@Resource
private UserService userServiceImplA;
//---------------------
@Resource(name = "userServiceImplA")
private UserService userService;
//---------------------
@Resource(type = UserServiceImplB.class)
private UserService userService;
//---------------------
@Qualifier(value = "userServiceImplA")
@Resource
private UserService userService;

5 appendix

5.1 Related basic code

public interface UserService {
}
@Service
public class UserServiceImplA implements UserService {
}

@Service
public class UserServiceImplB implements UserService {
}

@Service
public class OtherServiceImpl {
}

Leave a Reply

Your email address will not be published.

en_USEnglish