@Resource和@Autowired的区别

这篇具有很好参考价值的文章主要介绍了@Resource和@Autowired的区别。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.相同点

@Resource和@Autowired这两个注解的作用都是在Spring生态里面去实现Bean的依赖注入

2.不同点

2.1 @Autowired

首先,@Autowired是Spring里面提供的一个注解,默认是根据类型来实现Bean的依赖注入。
@Autowired注解里面有一个required属性默认值是true,表示强制要求bean实例的注入,在应用启动的时候,如果IOC容器里面不存在对应类型的Bean,就会报错。当然,如果不希望自动注入,可以把这个属性设置成false。

@Controller
public class HelloController {

    @Autowired(required = true)
    private HelloService helloService;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return helloService.hello();
    }
}

但是当项目有多个相同类型的Bean被定义时,使用@Autowired会报错

@org.springframework.context.annotation.Configuration
public class Configuration {
    @Bean("hello1")
    public HelloService hello1() {
        return new HelloServiceImpl();
    }

    @Bean("hello2")
    public HelloService hello2() {
        return new HelloServiceImpl();
    }
}

所以Spring启动的时候,会提示一个错误,大概意思原本只能注入一个单实例Bean,但是在IOC容器里面却发现有多个,导致注入失败。

Field helloService in com.zte.helloworld.contorller.HelloController required a single bean, but 3 were found:
	- helloServiceImpl: defined in file [D:\JavaWorkSpace\code\helloworld\target\classes\com\zte\helloworld\service\impl\HelloServiceImpl.class]
	- hello1: defined by method 'hello1' in class path resource [com/zte/helloworld/config/Configuration.class]
	- hello2: defined by method 'hello2' in class path resource [com/zte/helloworld/config/Configuration.class]

Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


针对这个问题,我们可以使用 @Primary或者@Qualifier这两个注解来解决。
@Primary表示主要的bean,当存在多个相同类型的Bean的时候,优先使用声明了@Primary的Bean。
@Qualifier的作用类似于条件筛选,它可以根据Bean的名字找到需要装配的目标Bean。

@Controller
public class HelloController {

    @Autowired(required = true)
    @Qualifier("hello1")
    private HelloService helloService;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return helloService.hello();
    }
}

2.2 @Resource

@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持。
它的使用方式和@Autowired完全相同,最大的差异于@Resource可以支持ByName和ByType两种注入方式。
如果使用name,Spring就根据bean的名字进行依赖注入,如果使用type,Spring就根据类型实现依赖注入。
如果两个属性都没配置,就先根据定义的属性名字去匹配,如果没匹配成功,再根据类型匹配。两个都没匹配到,就报错。文章来源地址https://www.toymoban.com/news/detail-473447.html

@Controller
public class HelloController {

//    @Autowired(required = true)
//    @Qualifier("hello1")
//    private HelloService helloService;
    @Resource(name = "helloService01")
    private HelloService01 helloService01;

    @Resource(type = HelloService02.class)
    private HelloService02 helloService02;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return helloService02.hello();
    }
}

3.总结

  • @Autowired是根据type来匹配,@Resource可以根据name和type来匹配,默认是name匹配。
  • @Autowired是Spring定义的注解,@Resource是JSR 250规范里面定义的注解,而Spring对JSR 250规范提供了支持。
  • @Autowired如果需要支持name匹配,就需要配合@Primary或者@Qualifier来实现。

到了这里,关于@Resource和@Autowired的区别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • @Resource和@Autowired的区别

    @Resource和@Autowired这两个注解的作用都是在Spring生态里面去实现Bean的依赖注入 首先,@Autowired是Spring里面提供的一个注解,默认是根据类型来实现Bean的依赖注入。 @Autowired注解里面有一个required属性默认值是true,表示强制要求bean实例的注入,在应用启动的时候,如果IOC容器里

    2024年02月08日
    浏览(30)
  • autowired和resource注解的区别

    注入规则不同 Autowired注解是spring的注解,此注解只根据type进行注入,不会去匹配name.但是如果只根据type无法辨别注入对象时,就需要配合使用@Qualifier注解或者@Primary注解使用。 Resource注解有两个重要的属性,分别是name和type,如果name属性有值,则使用byName的自动注入策略,将值

    2024年02月10日
    浏览(35)
  • @Autowired 和 @Resource 的区别是什么?

    Java面试题目录 @Autowired 和 @Resource 的区别是什么? @Autowired 是 Spring 提供的注解。默认的注入方式为byType(根据类型进行匹配)。 @Resource 是 JDK 提供的注解。默认注入方式为 byName(根据名称进行匹配)。 当一个接口存在多个实现类的情况下,@Autowired 和@Resource都需要通过名称

    2024年01月19日
    浏览(33)
  • 简述@Autowired和@Resource的区别(通俗易懂)

    前言:在日常的开发项目当中,这两个注解是经常会用到的,但是在实际使用当中好像使用起来并没有多大区别,这里我就对这两个注解进行一个详细的区别总结,通过一个完整的典型例子进行论证,思路清晰明了。 目录 一、结论 二、典型案例 2.1、准备代码 2.2、使用@A

    2024年02月03日
    浏览(38)
  • 每日一面系列-spring中@Autowired 和 @Resource 区别?

    @Autowired注解是由Spring提供的 ,它可以用来对构造方法、成员变量及方法参数进行标注,它能够根据对象类型完成自动注入,代码如下。 再来看@Resource注解,代码如下。 public class Service { @Resource(name = \\\"service1\\\") private Service service1; @Resource(name = \\\"service2\\\") private Service service2; @Reo

    2024年02月12日
    浏览(34)
  • 五个维度,解析 Spring 中 @Autowired 和 @Resource 的区别

    @Autowired注解是由Spring提供的,它可以用来对构造方法、成员变量及方法参数进行标注,它能够根据对象类型完成自动注入,代码如下: 再来看@Resource注解,代码如下: 它是由JDK提供的,遵循JSR-250规范,是JDK 1.6及以上加入的新特性。作为Java的标准,它的作用和@Autowired无区别

    2024年02月12日
    浏览(32)
  • 【Spring实战】28 @Autowired 和 @Resource注解的区别与使用

    Spring 框架是一个强大的 Java 企业应用开发框架, 提供了多种依赖注入的方式 。其中, @Autowired 和 @Resource 是两个常用的注解,用于实现依赖注入。本文将介绍这两个注解的区别以及在实际应用中如何进行选择。 1)匹配方式 @Autowired 是由 Spring 框架提供的注解,用于实现自动

    2024年01月21日
    浏览(45)
  • @Autowired 和 @Resource的区别只知道注入方式不同?那可不行,其性能上也有差距!

    目录 Autowire vs Resource 性能比较 先上结论: @Resource查找Bean的时间复杂度为O(1): @Autowired查找Bean的时间复杂度为O(n): 不能将所有的@Resource无脑替换成@Autowired 结合源码分析Autowire vs Resource 性能比较 @Autowire注解的处理地方: org.springframework.beans.factory.annotation.AutowiredAnnotationBea

    2024年02月06日
    浏览(33)
  • @Autowired和@Resource注解之间的关系区别,Bean的作用域和生命周期,Spring的执行流程

    目录 一. @Autowired 和 @Resource 注解 二. Bean的作用域  1. singleton(单例模式) 2. prototype(原型模式)(多例模式) 3. 请求作用域:request 4. 会话作用域:session 三. Spring 的执行流程 四. Bean 的生命周期   1. 实例化  2. 设置属性  3. Bean 初始化      3.1 执行各种各种 Aware 通知;     

    2024年02月04日
    浏览(48)
  • Spring高手之路-@Autowired和@Resource注解异同点

    目录 概述 相同点 1.都可以实现依赖注入 2.都可以用于注入任意类型的Bean 3.都支持通过名称、类型匹配进行注入 不同点 1.来源不同。 2.包含的属性不同 3.匹配方式(装配顺序)不同。 4.支持的注入对象类型不同 5.应用地方不同 @Autowired 和 @Resource 是在 Java 开发中用于实现依赖

    2024年02月03日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包