源无极

导航

 

@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。

1、共同点

两者都可以写在字段和setter方法上。两者如果都写在字段上,那么就不需要再写setter方法。

2、不同点

(1)@Autowired

查看源码:

package org.springframework.beans.factory.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
public @interface Autowired {
 boolean required() default true;
}

 

 

@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。

 1 public class TestServiceImpl {
 2  // 下面两种@Autowired只要使用一种即可
 3  @Autowired
 4  private UserDao userDao; // 用于字段上
 5  
 6  @Autowired
 7  public void setUserDao(UserDao userDao) { // 用于属性的方法上
 8  this.userDao = userDao;
 9  }
10 }

 

 

@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起使用。如下:

1 public class TestServiceImpl {
2  @Autowired
3  @Qualifier("userDao")
4  private UserDao userDao; 
5 }

 

 

    当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。来看下面的代码: 对方法

 1 public class Boss { 
 2  private Car car; 
 3  private Office office; 
 4  
 5  @Autowired 
 6  public void setCar(Car car) { 
 7  this.car = car; 
 8  } 
 9  
10  @Autowired 
11  public void setOffice(Office office) { 
12  this.office = office; 
13  } 
14 15 } 

 

 

这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:

 1 public class Boss { 
 2  private Car car; 
 3  private Office office; 
 4  
 5  @Autowired 
 6  public Boss(Car car ,Office office){ 
 7  this.car = car; 
 8  this.office = office ; 
 9  } 
10  
11 12 } 

 

 

(2)@Resource

查看源码:

 1 package javax.annotation;
 2 import java.lang.annotation.*;
 3 import static java.lang.annotation.ElementType.*;
 4 import static java.lang.annotation.RetentionPolicy.*;
 5 @Target({TYPE, FIELD, METHOD})
 6 @Retention(RUNTIME)
 7 public @interface Resource {
 8  String name() default "";
 9  String lookup() default "";
10  Class type() default java.lang.Object.class;
11  enum AuthenticationType {
12  CONTAINER,
13  APPLICATION
14  }
15  AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
16  boolean shareable() default true;
17  String mappedName() default "";
18  String description() default "";
19 }

 

 

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

注:最好是将@Resource放在setter方法上,因为这样更符合面向对象的思想,通过set、get去操作属性,而不是直接去操作属性。

@Resource装配顺序:

①如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。

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

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

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

例如以下写法都对:(当然下面只是写在成员变量中,也可以写在set方法上)

1 @Resource(type = cn.qlq.service.UserService.class)
2  private UserService userService;
3 
4  @Resource(name="userServiceImpl")
5  private UserService userService;
6 
7  @Resource
8  private UserService userService;

 

 

总结:

  • @Resource默认按照名称方式进行bean匹配(byName),@Autowired默认按照类型方式进行bean匹配(byType)
  • @Resource(import javax.annotation.Resource;)是J2EE的注解,
  • @Autowired( import org.springframework.beans.factory.annotation.Autowired;)是Spring的注解

Spring属于第三方的,J2EE是Java自己的东西。使用@Resource可以减少代码和Spring之间的耦合

转载地址:https://www.toutiao.com/i6628176151485350404/

posted on 2018-11-29 22:01  源无极  阅读(78)  评论(0编辑  收藏  举报