@resource与@authorwired在本质上它们的作用是一样的,都是省去为一个对象变量写get,set方法,自动为这个对象注入实例化对象即注入依赖。而它们的注入的方式还是有所区别的

@resource是基于j2ee的注解,默认是按名字进行注解,若不指定装配bean的名字,当注解写在字段上时,默认取字段名,按照名称查找通过set方法进行装配,倘若有多个子类,则会报错。若想不报错,只需加(required=false)
例如:

package com.chexiang.common;public class Customer
{       @Resourceprivate Person person;public Customer(Person person) {this.person = person;}public void setPerson(Person person) {this.person = person;}//...
}
package com.chexiang.common;
@Component
public class Person
{//...
}
其对应的xml装配过程是如下,先定义customer的bean,然后申明装配方式是byname,再定义一个person类的bean。这是id名为customer的bean对象的对象属性会自动按照属性名去找寻id名与属性名一样的bean,则id为person的bean被注入到了customer的person变量里
<bean id="customer" class="com.chexiang.common.Customer" autowire="byName" />
<bean id="person" class="com.chexiang.common.Person" />

@autowired是基于spring的注解org.springframework.beans.factory.annotation.Autowired,它默认是按类型进行的装配的,如果想要它按名字进行装配只需在@autowired下面加@qualifier("name")`注解,其对应的xml装配方式如下

两个Bean,person 和 ability.
package com.chexiang.common;public class Person
{       @autowired
   private Ability ability;//...
}
package com.chexiang.common;
@Component
public class Ability
{private String skill;//...
}
<!-- person has a property type of class "ability" --><bean id="person" class="com.yiibai.common.Person" autowire="byType" /><bean id="invisible" class="com.yiibai.common.Ability" ><property name="skill" value="Invisible" /></bean>

id名为person的bean会把ability属性按照bean里定义的class的类型进行装配,因为id名为invisible的bean的class类型与id名为person的变量ability的类型相同,则进行装配。

@resource与@autowired的区别相关推荐

  1. spring 注解说明以及@Resource和@Autowired的区别

    2019独角兽企业重金招聘Python工程师标准>>> 一.spring常见的注解有 @Component.@Repository.@Service.@Controller @Aut ...

  2. @Resource和@Autowired的区别

    前言 @Resource和@Autowired都是做bean的注入时使用,@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入. 其中@Resourc ...

  3. @Resource与@Autowired用法区别

    spring中,@Resource和@Autowired都是做bean的注入时使用.使用过程中,有时候@Resource 和 @Autowired可以替换使用:有时,则不可以. 下面,根据自己的学习, ...

  4. Spring中@Resource与@Autowired、@Qualifier的用法与区别

    Spring中@Resource与@Autowired.@Qualifier的用法与区别 1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法 ...

  5. Spring注解@Resource和@Autowired区别对比

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...

  6. 同一接口有多个实现类,怎么来注入一个指定的实现?@Resource、@Autowired、@Qualifier

    如果一个接口有2个以上不同的实现类, 那么如何Autowire一个指定的实现 1:首先,UserService接口有两个实现类 UserService1和 UserService2 UserServi ...

  7. spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?

    问题:spring中@Inject和@Autowired的区别?分别在什么条件下使用呢? 我在浏览SpringSource上的一些博客,在其他一个博客中,那个作者用了@Inject,但是我觉得他用@A ...

  8. Spring 注解 @Resource和@Autowired(转)

    鸣谢:http://my.oschina.net/u/216467/blog/205951 @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spri ...

  9. Spring 注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...

最新文章

  1. HDU1285拓扑排序模版题
  2. jquery问题,如何调用带this的函数?
  3. 基于Springboot2.0的Dubbo入门项目(dubbo-spring-boot-starter)
  4. bootstrap --- 标签页切换
  5. SOA架构师注意的问题
  6. 如何用python 开发web_如何用python开发web
  7. vue 打包路由报错_Vue下路由History模式打包后页面空白的解决方法
  8. C++ STL string的输出
  9. ubuntu 12.04安装与配置
  10. html css 表头,css固定表格表头(各浏览器通用)
  11. 真牛!打开mysql
  12. ORACLE查询表最近更改数据的方法
  13. vc mscomm串口通信使用了CButtonST按钮类软件分析
  14. JavaScript【面向对象】-静态方法-私有方法-公有方法-特权方法
  15. node服务器返回文件,Node.js 服务器对应的路由返回对应的 HTML 文件
  16. 大数据新闻推送你怎么看_人工智能选角大数据推送
  17. 离开百度的50余位AI大牛,假如组一个这样的公司……
  18. html 小喇叭图标,小喇叭不见了怎么办(小喇叭图标不见了的原因及解决办法)...
  19. Linux下的man指令
  20. linux sticky权限,Linux基础入门篇之文件高级权限suid,sgid,sticky

热门文章

  1. 一个关于高考的黑客故事:用2B铅笔注入阅卷系统
  2. Scopus使用技巧
  3. 企业数字化是手段,中国企业商业创新是目的。但商业创新的重点是什么?
  4. SSM研究生推免网站
  5. 健身中心健身管理系统的设计与实现(源码+数据脚本+论文+技术文档)
  6. Docker容器(ubuntu)使用VNC设置远程桌面,在Windows中显示
  7. 计算机等级证书等级及报考条件
  8. 蓝桥杯题库 BASIC-3 字母图形
  9. 六:Grafana-Boom Theme用法
  10. 他打算上计算机课程 英文翻译,计算机专业课程名称英文翻译