@Autowired和@Resource注解超详细总结(附代码)

区别

1、来源不同

@Autowired 和@ Resource 注解来自不同的“父类”,其中@Autowired注解是 Spring 定义的注解,而@Resource 注解是 Java 定义的注解,它来自于 JSR-250(Java 250 规范提案)。

2、支持的参数不同

@Autowired注解只支持设置 1 个参数,而@Resource注解支持设置 7 个参数。

public @interface Autowired {
    boolean required() default true;
}


public @interface Resource {
    String name() default "";
    
    String lookup() default "";
    
    Class<?> type() default java.lang.Object.class;
    
    enum AuthenticationType {
            CONTAINER,
            APPLICATION
    }
    
    AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
    
    boolean shareable() default true;
    
    String mappedName() default "";
    
    String description() default "";
}

3、注入规则不同(重要)

首先我们先明确我们在声明Bean的时候的类型type名称name到底是什么?
首先type是比较好理解的,我们在声明Bean的时候方法的返回类型就是这个Bean的类型
先贴上我测试的代码:

----------Bean实体类
@Data
public class HelloService {
    private String name;

    public HelloService(String name) {
        this.name = name;
    }
}



----------配置类
@Configuration
public class MyConfig {

    @Bean
    public HelloService hello1() {
        return new HelloService("i'm hello1");
    }

    @Bean
    public HelloService hello2() {
        return new HelloService("i'm hello2");
    }
}

----------服务类
@Service
public class UserService {

    @Autowired
    private HelloService helloService;

    public void method() {
        System.out.println(helloService.getName());
        System.out.println(helloService.toString());
    }
}
--------测试类
public class MyTest {
    @Autowired
    private UserService userService;

    @Test
    public void myTest() {
        userService.method();
    }
}




按照以上代码服务类中的@Autowired会报错,类型错误,并且idea给出了建议qualifier
![在这里插入图片描述](https://img-blog.csdnimg.cn/bda4ee6321544a2ca67cb46c78329298.png

public @interface Bean {
	/**
	 * Alias for {@link #name}.
	 * <p>Intended to be used when no other attributes are needed, for example:
	 * {@code @Bean("customBeanName")}.
	 * @since 4.3.3
	 * @see #name
	 */
	@AliasFor("name")
	String[] value() default {};

	/**
	 * The name of this bean, or if several names, a primary bean name plus aliases.
	 * <p>If left unspecified, the name of the bean is the name of the annotated method.
	 * If specified, the method name is ignored.
	 * <p>The bean name and aliases may also be configured via the {@link #value}
	 * attribute if no other attributes are declared.
	 * @see #value
	 */
	@AliasFor("value")
	String[] name() default {};
	}

@Bean注解中的name属性说明了:

此 Bean 的名称,如果有多个名称,则为主要 Bean 名称加上别名。
如果未指定,Bean 的名称就是注释方法的名称。如果指定,方法名称将被忽略。
如果没有声明其他属性,也可以通过 value 属性配置 Bean 名称和别名。

其实总结就是说:name在@Bean中如果给了值,则需要注入的地方就要使用此name值,如果没有给则使用方法名,我的例子中就是默认两个bean的名字为hello1、hello2

理解了name 和 type 对于有多个相同类型的bean注入就很好理解了,@Autowired和@Resource也提供了解决方式。

@Autowired注解是Spring的注解,此注解只根据type进行注入,不会去匹配name.但是如果只根据type无法辨别注入对象时,就需要配合使用@Qualifier注解或者@Primary注解使用。

下面给出搭配这两个注解解决错误的代码

在配置类中给hello1使用@Primary,意思就是有多个类型相同的Bean时,加了此注解的是大哥!
    @Bean
    @Primary
    public HelloService hello1() {
        return new HelloService("i'm hello1");
    }
    @Autowired
    @Qualifier(value = "hello1") //给定要指定的Bean的名称,我们默认的是hello1 hello2
    private HelloService helloService;

    public void method() {
        System.out.println(helloService.getName());
        System.out.println(helloService.toString());
    }

在这里插入图片描述
结果是一样的:
在这里插入图片描述

@Resource注解有两个重要的属性,分别是name和type,如果name属性有值,则使用byName的自动注入策略,将值作为需要注入Bean的名字,如果type有值,则使用byType自动注入策略,将值作为需要注入Bean的类型。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。即@Resource注解默认按照名称进行匹配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,当找不到与名称匹配的Bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

  @Resource
    private HelloService helloService;

    public void method() {
        System.out.println(helloService.getName());
        System.out.println(helloService.toString());
    }

按照上述写法,idea不会报错,运行时就发现了不能够单一匹配错误No qualifying bean of type 'com.xxx.mytest.HelloService' available: expected single matching bean but found 2: hello1,hello2

根据@Resource的属性,我们很简单的可以使用name来指定Bean

	@Resource(name = "hello2")
	 //name指定Bean,如前面所说,如果@Bean(name = "xxx"),注入时也需要按照指定name
    private HelloService helloService;

    public void method() {
        System.out.println(helloService.getName());
        System.out.println(helloService.toString());
    }
 	@Resource(name = "pickme")
 	//name指定Bean,如前面所说,如果@Bean(name = "xxx"),注入时也需要按照指定name
    private HelloService helloService;

    public void method() {
        System.out.println(helloService.getName());
        System.out.println(helloService.toString());
    }

结果相同:
在这里插入图片描述

4、依赖查找的顺序不同

@Autowired注解先根据类型(byType)查找,如果存在多个(Bean)再根据名称(byName)进行查找;
@Resource注解先根据名称(byName)查找,如果(根据名称)查找不到,再根据类型(byType)进行查找。

5、依赖注入的用法支持不同

@Autowired注解支持属性注入构造方法注入Setter 注入,而@Resource注解只支持属性注入Setter 注入

	// 属性注入 上述方式
    @Autowired
    private UserService userService;
----------------------------------------------------
    // 构造方法注入
public class UserController {

    private UserService userService;
 
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
}
--------------------------------------------------
    // Setter 注入
public class UserController {

    private UserService userService;
 
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

6、编译器 IDEA 的提示不同

当使用 IDEA 专业版在编写依赖注入的代码时,如果注入的是 Mapper 对象,那么使用@Autowired注解编译器会提示报错信息。虽然 IDEA 会出现报错信息,但程序是可以正常执行的。 然后,我们再将依赖注入的注解更改为@Resource注解就不会出现报错信息了

7、使用位置不同

两者都可以写在字段和setter方法上,如果写在字段上,那么就不需要在写setter方法。推荐使用@Resource注解在字段上,这样不仅不需要写setter方法了,而且由于@Resource注解属于J2EE,降低与spring的耦合。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BiuPsYao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值