@Autowired 和 @Resource深入分析
我们在开发中,一直用到的都是@Autowired,那么为什么这么用有没有想过?还有@Resource也可以作为注入的注解,为什么又不用?
“认祖归宗”–@Autowired 和 @Resource来源
那么首先,我们得知道这两个注解,他们是来自于哪?
@Autowired
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({
ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}

本文深入探讨了Spring的@Autowired和J2EE的@Resource注解的来源、作用及区别。@Autowired默认按类型注入,当有多个匹配bean时,可通过@Qualifier指定。而@Resource默认按名称注入,未指定时会按类型寻找,导致冲突。解决冲突可明确指定name属性。
最低0.47元/天 解锁文章
1万+





