@Resource和@Autowired的区别

news/2025/3/26 16:20:34/

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就根据类型实现依赖注入。
如果两个属性都没配置,就先根据定义的属性名字去匹配,如果没匹配成功,再根据类型匹配。两个都没匹配到,就报错。

@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来实现。

http://www.ppmy.cn/news/97644.html

相关文章

MessageBoxA-函数原型-消息提示-显示为顶层窗口

文章目录 1.函数功能2.返回值3.示例 1.函数功能 显示一个模式对话框,其中包含系统图标、一组按钮和一条简短的应用程序特定消息,例如状态或错误信息。 消息框返回一个整数值,该值指示用户单击的按钮。 int MessageBoxA([in, optional] HWND…

【MySQL新手到通关】第六章 时间日期函数

文章目录 1.获取日期时间函数1.1 获取当前日期时间1.2 获取当前日期1.3 获取当前时间 2.日期格式化★★★2.1 日期转指定格式字符串2.2 字符串转日期 3.日期间隔3.1 增加日期间隔 ★★★3.2 减去一个时间间隔★★★3.3 日期相差天数(天)3.4 相差时间&…

1105 Spiral Matrix(32行代码+详细注释)

分数 25 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then…

关于强电与弱的的介绍

强电?弱电?傻傻分不清楚,今天海翎光电的小编为大家系统的介绍一下强电与弱电。 什么是强电? (1)供配电系统:供配电系统包括负荷分级、供电措施、负荷力矩、电网谐波限值、用电指标、负荷所需要…

RNN Seq2Seq

Feedforward v.s. Recurrent Feedforward network does not have input at each stepFeedforward network has different parameters for each layer 双向RNN 双向递归层可以提供更好的识别预测效果,但却不能实时预测,由于反向递归的计算需要从最末时刻…

【Servlet】

目录 🎂1. 第一个 Servlet 程序:使用 Servlet 写 hello world 🥞1.1 创建项目 🍳1.2 引入依赖 🎃1.3 创建目录 🍘1.4 开始写代码 🌍1.5 打包代码 🍤1.6 部署 👑1…

《C++程序设计原理与实践》笔记 第18章 向量和数组

本章将介绍如何拷贝以及通过下标访问向量。为此,我们讨论一般的拷贝技术,并考虑向量与底层数组表示之间的关系。我们将展示数组与指针的关系及其使用引发的问题。我们还将讨论对于每种类型必须考虑的五种基本操作:构造、默认构造、拷贝构造、…

python--杂识--9--subprocess.Popen()各参数含义

subprocess.Popen() 是一个非常有用的 Python 模块,它可以在当前进程内或者在子进程中运行系统命令,并能够查看返回结果。它的一般语法如下: subprocess.Popen(args, bufsize-1, executableNone, stdinNone, stdoutNone,stderrNone, preexec…