`

网上中文资料都说@Resource是byName注入,其实是byType注入

 
阅读更多

网上中文资料都说@Resource是byName注入,其实是byType注入。以讹传讹,有时候真的不靠谱啊。

 

http://www.coderanch.com/t/478706/Spring/Autowired-Qualifier-Resource-annotation

@Autowired is Spring's Annotation(only usable in Spring) 
@Resource is JSR-250 Common Annotation equivalent (works the same in Java EE 5) 
@Inject is JSR 33? Common Annotation equivalent. (works the same in Java EE 6?) 

All three do the same thing. However, @Autowired is the only annotation of the three that you can put on constructors. 

Now since auto wiring by default does it by object type. If you have two beans of the same type, then there is ambiguity that Spring can't figure out which of the two beans to inject. That is what @Qualifier is for, to use the bean name to fix the ambiguity and tell Spring which of the two beans to inject. Use @Qualifier with @Autowired. With @Resource and @Inject you can just put the bean name in the annotation as an attribute. 

Hope that helps 

Mark

验证了一下,还真是byType注入。

分享到:
评论
3 楼 宇落YL 2015-05-16  
是byName
指定type时byType 指定name时byName
两个都指定时,找到type和name都符合条件的bean
不指定时默认就是byName
2 楼 llade 2012-03-29  
freeroy 写道
resource 传入name属性后,就是按名注入了

对的,我说的是默认情况。
1 楼 freeroy 2012-03-29  
resource 传入name属性后,就是按名注入了

相关推荐

    spring中的自动装配实例byName、byType

    spring中的自动装配实例byName、byType,两种装配方式的实例。有需要学习的朋友可以看一下。

    基于注解的DI.docx

    常用注解: @Component、创建对象 @Respotory、创建dao对象,用来访问数据库 @Service、创建Service对象,处理...@Resource、jdk中的注解,使用自动注入给引用数据类型赋值,支持byName、byType,默认byName

    springMVC-annotation注解介绍

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...

    spring基础

    @Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name...

    autowiring-benchmark:SPR-13086 的基准应用程序

    模式 5 @Resource (byName) 如何测量 运行脚本。 $ sh run.sh N 这个基准测试应用程序有 N 个Controller s、 Service s、 ServiceImpl s 和每个模式的一个Main端点。 当 N=100 时,会生成 (100 + 100 + 100 + 1) ...

    spring自动装配例子

    (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean class="xxx" ></bean> (4)constructor:与 ...

    Spring的三种注入方式

    2.autowire=“byName”  3.autowire=bytype    详细解析注入方式  例如:有如下两个类需要注入  第一个类: 1.package org.jia;  2.  3. public class Order {  4. private String orderNum;...

    Spring的学习笔记

    (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 ...

    spring2.5 学习笔记

    (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:annotation方式Spring 16 一、 ...

    spring-learn:Spring轻松学习demo

    byName根据属性名称自动装配-set注入 byType在容器中查找指定属性类型进行装配-set注入 构造函数与byType类似-构造方法注入 5.豆的注解 @Autowired可以注解列表和地图; @Order(1)更改注解顺序,list中有效 @...

    xmljava系统源码-springdemo:spring3的IOC,AOP,JDBC,orm等各个模块的使用示例集锦,可以作为入门的spri

    byname,bytype,default * 5.annotation(autowired,resource,@Qualifier) * 6.bean的生命周期 init-method,destroy-method,initiableBean, DisposableBean,lasyinit,singleton,prototype @PostConstruct @P

    Spring_1_IOC

    Spring_1_IOC, 1.简单的依赖注入。 2.对象的生命周期:单例/原型。 3.依赖注入的两种方式:设值注入/构造注入。 4.对象工厂的延迟创建。 5.自动装配:byName/byType。

    spring4框架系列 [ 4 ]

    spring4框架系列 [ 4 ] 基于xml注入包含setter、构造、命名空间、集合属性、byName、byType、SPEL、匿名Bean、同类抽象、异类抽象、配置多子配置文件

    spring-autowire-demo.zip

    以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比

    Spring

    全文大纲1、Spring1.1、简介1.2、优点1.3、组成(七大模块)1.4、拓展2、IOC理论...注入6.4、Bean的作用域7、Bean的自动装配7.1、ByName自动装配7.2、ByType自动装配7.3、使用注解实现自动装配8、使用注解开发9、使用

    xmljava系统源码-SpringInAction4:《SpringInAction4th》学习笔记

    byName, byType, contructor, autodetect 可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire) 如果使用constructor自动装配策略,就不能混合使用constructor-arg ...

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...

    Spring的自动装配源代码

    spring中的自动装配的形式byType、byName、no、construtor、autodetect和default(beans标签中没有,而bean标签中有该属性)共6种装配的方式的举例说明

Global site tag (gtag.js) - Google Analytics