spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖。在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入。虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区 别的。首先来看一下:

  • @Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;
  • @Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;
  • @Resource注解是又J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用@Resource的方式;
  • @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上。
  1. 使用注解的方式,我们需要修改spring配置文件的头信息,修改部分红色标注,如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
  2. 修改以上配置文件的头信息后,我们就可以在Java代码通过注解方式来注入bean,看下面代码:
  • @Resource
 public class StudentService3 implements IStudentService {

   //@Resource(name="studentDao")放在此处也是可行的
private IStudentDao studentDao;
private String id;
public void setId(String id) {
this.id = id;
} @Resource(name="studentDao") // 通过此注解完成从spring配置文件中查找名称为studentDao的bean来装配字段studentDao,如果spring配置文件中不存在 studentDao名称的bean则转向按照bean类型经行查找
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
} public void saveStudent() {
studentDao.saveStudent();
System.out.print(",ID 为:"+id);
} }
 配置文件添加如下信息

 <bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>
<bean id="studentService3" class="com.wch.service.impl.StudentService3" /
  • Autowired
 public class StudentService3 implements IStudentService {

   //@Autowired放在此处也是可行的
private IStudentDao studentDao; private String id; public void setId(String id) {
this.id = id;
} @Autowired//通过此注解完成从spring配置文件中 查找满足studentDao类型的bean //@Qualifier("studentDao")则按照名称经行来查找转配的
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
} public void saveStudent() {
studentDao.saveStudent();
System.out.print(",ID 为:"+id);
} }

配置文件添加如下信息:

<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>
<bean id="studentService3" class="com.wch.service.impl.StudentService3" />

在java代码中可以使用@Autowire或者@Resource注解方式进行装配,这两个注解的区别是:
@Autowire 默认按照类型装配,默认情况下它要求依赖对象必须存在如果允许为null,可以设置它required属性为false,如果我们想使用按照名称装配,可 以结合@Qualifier注解一起使用;

@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属
性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找
依赖对象.

注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖的对象时候,会回退到按照类型装配,但一旦指定了name属性,就只能按照名称 装配了.

Spring @Resource,@Autowired,@Qualifier的注解注入和区别的更多相关文章

  1. Spring @Resource、@Autowired、@Qualifier的注解注入及区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  2. @Resource、@Autowired、@Qualifier的注解注入及区别

    在Java代码中可以使用 @Resource  或者 @Autowired 注解方式来进行注入. 虽然 @Resource 和 @Autowried 都可以完成依赖注入,但是他们是有区别的. 一: @ ...

  3. 三分钟学会@Autowired@Qualifier@Primary注解

    三分钟学会@Autowired@Qualifier@Primary注解 2018.10.08 20:24 154浏览 今天主要简单的跟大家介绍一下spring自动装配相关的@Autowired,@Qu ...

  4. spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差异

    为了探寻 '@Resource', '@Autowired', 和'@Inject'如何解决依赖注入中的问题,我创建了一个"Party"接口,和它的两个实现类"Perso ...

  5. Spring @Resource, @Autowired and @Inject 注入

    Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...

  6. spring @resource @ Autowired

    Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类 ...

  7. spring使用@Autowired为抽象父类注入依赖

    有时候为了管理或者避免不一致性,希望具体服务统一继承抽象父类,同时使用@Autowired为抽象父类注入依赖.搜了了网上,有些解决方法实现实在不敢恭维,靠子类去注入依赖,那还有什么意义,如下: 父类: ...

  8. Spring(四)使用注解注入Bean

    注解简单介绍 是代码里面的特殊标记,使用注解完成功能. 注解写法@ 注解名称(属性名=属性值). 注解可以作用在类.方法.属性上面. 使用流程: 在ApplicationContext.xml中开启注 ...

  9. [Spring]IoC容器之进击的注解

    先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...

随机推荐

  1. 用python画小王八裤(turtle库)

    一,采用Python语言如何画一朵玫瑰花 工具/原料 Python语言包 Win10 一. 准备 1. 打开界面: 打开python 2. 创建文件 二. 编程 1. 编写画图: from turtl ...

  2. 最短路径之Bellman-Ford——解决负权边

    Bellman-Ford算法非常简单,核心代码四行,可以完美的解决带有负权边的图. for(k=1;k<=n-1;k++) //外循环循环n-1次,n为顶点个数 for(i=1;i<=m; ...

  3. Win32汇编学习(5):绘制文本2

    这次我们将学习有关文本的诸多属性如字体和颜色等. 理论: Windows 的颜色系统是用RGB值来表示的,R 代表红色,G 代表绿色,B 代表蓝色.如果您想指定一种颜色就必须给该颜色赋相关的 RGB ...

  4. Bytom合约预编译

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 在开发合 ...

  5. 【Git】git error记录之 "unpacking the sent packfile failed on the remote"

    错误信息: error: cannot open .git/FETCH_HEAD: Permission denied unpacking the sent packfile failed on th ...

  6. Qt Windows打开指定文件注意替换双斜杠为单斜杠

    QProcess::startDetached(QString("explorer %1").arg(strFilePath)); 其中,在windows上使用时,strFileP ...

  7. Selenium(基于JAVA语言)-》在eclipse上运行web项目在Mac系统上启动时提示nodename nor servname provided解决办法

    最近使用eclipse进行自动化测试时,遇到一种情况,无法调起浏览器,且有报错,如下: org.openqa.selenium.WebDriverException: failed to lookup ...

  8. robot framework学习二-----元素定位

    文章摘自:https://www.cnblogs.com/fnng/p/3901391.html 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot fram ...

  9. Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready

    Getting Node.js Apps Production Ready 部署程序需要知道的方面: Environment variables Express.js in production So ...

  10. JavaScript基础二

    1.7 常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象,我们知道对象都是有相应的属性和方法 1.7.1 数组Array 1.数组的创建方式 字面量方式创建(推荐大家使用这种方式, ...