Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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
Java 这两种春季IOC注射有什么区别?_Java_Spring - Fatal编程技术网

Java 这两种春季IOC注射有什么区别?

Java 这两种春季IOC注射有什么区别?,java,spring,Java,Spring,这两种注射有什么区别 @Autowired private DocumentDAO documentDao; @Resource(name = "documentDao") private DocumentDAO documentDao; 简单地说,@Autowired(Spring中的规范)按类型连接,@Resource(JSR-250中的规范)按名称连接 但是,@Autowired with@Qualifier也可以将Autowired命名为@Resource 请查看以下链接: 默认

这两种注射有什么区别

@Autowired
private DocumentDAO documentDao;

@Resource(name = "documentDao")
private DocumentDAO documentDao;
简单地说,@Autowired(Spring中的规范)按类型连接,@Resource(JSR-250中的规范)按名称连接

但是,@Autowired with@Qualifier也可以将Autowired命名为@Resource

请查看以下链接:


默认情况下
@Autowire
按类型注入依赖项。但它也可以使用
@Qualifier
结合
@Autowire
注释“按名称”注入依赖项


但关键的区别在于,
@Autowired
是一个spring注释,而
@Resource
是由JSR-250指定的。因此,
@Resource
是另一边普通Java的一部分,
@Autowired
只能在Spring之前提供。

谢谢大家的快速回复,dgregory和rohan,你们都在同一条线上。但是@dgregory首先:)回答得很好。谢谢