Detailed explanation of the difference between Spring annotation @Resource and @Autowired

Preface

@Resource and @Autowired are both used for bean injection. In fact, @Resource is not an annotation of Spring. Its package is javax.annotation.Resource and needs to be imported, but Spring supports injection of this annotation.

1. Common ground

Both can be written on fields and setter methods. If both are written on the field, then there is no need to write setter methods.

2. The difference

(1)@Autowired

@Autowired is the annotation provided by Spring, which needs to be imported into the package org.springframework.beans.factory.annotation.Autowired; it is only injected according to byType.

1

2

3

4

5

6

7

8

9

10

public class TestServiceImpl {

  // 下面两种@Autowired只要使用一种即可

  @Autowired

  private UserDao userDao; // 用于字段上

   

  @Autowired

  public void setUserDao(UserDao userDao) { // 用于属性的方法上

    this.userDao = userDao;

  }

}

@Autowired annotation is to assemble dependent objects according to type (byType). By default, it requires dependent objects to exist. If null values ​​are allowed, you can set its required attribute to false. If we want to use byName to assemble, we can use it in conjunction with the @Qualifier annotation. as follows:

1

2

3

4

5

public class TestServiceImpl {

  @Autowired

  @Qualifier("userDao")

  private UserDao userDao;

}

(2)@Resource

@Resource is automatically injected according to 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 parses the name attribute annotated with @Resource as the name of the bean, and the type attribute parses as the type of the bean.

Therefore, if the name attribute is used, the automatic injection strategy of byName is used, and the automatic injection strategy of byType is used when the type attribute is used. If neither the name nor the type attribute is specified, then the byName automatic injection strategy will be used through the reflection mechanism.

1

2

3

4

5

6

7

8

9

10

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, to manipulate properties through set and get instead of directly operating properties.

@Resource assembly sequence:

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

②如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常。

③如果指定了type,则从上下文中找到类似匹配的唯一bean进行装配,找不到或是找到多个,都会抛出异常。

④如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。

@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。

Guess you like

Origin blog.csdn.net/xiaokanfuchen86/article/details/113837062
Recommended