Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @Resource和@Autowired的区别_Spring - Fatal编程技术网

Spring @Resource和@Autowired的区别

Spring @Resource和@Autowired的区别,spring,Spring,我不明白为什么在我用@Resource注释它时,下面的一行给出了一个错误,我在写@Autowired时没有得到这个错误 @Resource(name = "systemConfigService") private SystemConfigService systemConfigService; 它告诉我们,无法解析bean systemConfigService SystemConfigService是一个接口。@Autowired与@Qualifier结合使用,也按名称自动连接。主要区别在

我不明白为什么在我用@Resource注释它时,下面的一行给出了一个错误,我在写@Autowired时没有得到这个错误

@Resource(name = "systemConfigService")
private SystemConfigService systemConfigService;
它告诉我们,无法解析bean systemConfigService


SystemConfigService是一个接口。

@Autowired与@Qualifier结合使用,也按名称自动连接。主要区别在于@Autowired是一个spring注释,而@Resource是由JSR-250指定的。因此,后者是普通java的一部分,其中as@Autowired只能由spring提供

如果您打算用名称表示注释驱动的注入,请不要 主要使用@Autowired-即使在技术上能够引用 通过@Qualifier值创建一个bean名称。相反,更喜欢JSR-250 @在语义上定义的资源注释,用于标识 特定目标组件的唯一名称,以及声明的类型 与匹配过程无关

这种语义差异的一个具体结果是 定义为集合或映射类型的自身不能通过 @自动连线,因为类型匹配不适用于它们。使用 @资源,引用特定的集合/映射 使用唯一的名称创建bean


注意:与适用于字段、构造函数和多参数方法的@Autowired(允许通过参数级别的限定符注释缩小范围)不同,@Resource仅支持具有单个参数的字段和bean属性setter方法。因此,如果注入目标是构造函数或多参数方法,请坚持使用限定符。

显示SystemConfigServiceBean的配置如果使用
@Autowired(required=true)对其进行注释会发生什么情况。