Spring自动装配----注解装配----Spring自带的@Autowired注解

父类

package cn.ychx;public interface Person {public void sayHello();}

学生子类

package cn.ychx;public class Student implements Person {@Overridepublic void sayHello() {System.out.println("Hello! My name is Tom, I'm a Student.");}}

教师子类

package cn.ychx;public class Teacher implements Person {@Overridepublic void sayHello() {System.out.println("Hello! My name is Alice, I'm a Teacher.");}}

表演类

package cn.ychx;public interface Performer {public void perform();
}

玩游戏

package cn.ychx;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class PlayGame implements Performer {@Autowired(required=false)@Qualifier("student")private Person person;@Overridepublic void perform() {if (person == null) {System.out.println("Person is not exist.");return;}person.sayHello();}
}

配置文件

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- 启用注解支持 --><context:annotation-config/><bean id="student" class="cn.ychx.Student"/><bean id="teacher" class="cn.ychx.Teacher"/><bean id="playGame" class="cn.ychx.PlayGame"></bean>
</beans>

执行

package cn.ychx;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {private static ApplicationContext ctx;public static void main(String[] args) {ctx = new ClassPathXmlApplicationContext("applicationContext.xml");Performer performer = (Performer) ctx.getBean("playGame");if (performer != null) {performer.perform();} else {System.out.println("Object is null.");}}
}

执行结果

Hello! My name is Tom, I'm a Student.

通过修改@Qualifier("teacher")可以得到结果

Hello! My name is Alice, I'm a Teacher.

示例使用的是Spring自带的@Autowired注解,实现将Person的实现类(student或者teacher)注入PlayGame类的person属性中。

1.启用Spring注解支持

<context:annotation-config/>

2.@Autowired(required=false)注解

可以标注在属性,set方法,构造器上,使用byType寻找上下文的bean。可以看到示例中标注在属性上面,这时可以省略set方法,不受private的影响。

required=false说明Bean的注入不是必须的,也就是说可以删除配置文件中的student和teacher的Bean,这时PlayGame中的person属性为null

4.@Qualifier("student")注解

用来明确指定要使用的bean(双引号中的)是谁,为什么使用这个注解,因为可以满足PlayGame中的person属性的Bean不是唯一的,有两个分别是student和teacher,

这时Sping不知道该使用那个去注入,就会抛出异常。当使用@Qualifier("student")注解,就可以明确的告诉Spring在有多个Bean满足条件是用哪个。

5.缺点是会引入对Spring的特定依赖

转载于:https://www.cnblogs.com/yangchongxing/p/7642405.html

Spring自动装配----注解装配----Spring自带的@Autowired注解相关推荐

  1. Spring—自动装配与注解自动装配

    目录 自动装配 Spring支持5种自动装配模式 演示自动装配 1. no 默认装配方式 2.使用 byName 自动装配 3.使用 byType 自动装配 4.使用 constructor 自动装配 ...

  2. (二)Spring自动装配

    Spring自动装配 为了减少XML的配置数量.Spring提供了几种技巧来解决这一问题: 自动装配(autowiring): 有助于减少<property>元素和<constroc ...

  3. 学习篇(一)- Spring自动装配的方式

    Spring自动装配的方式 1. 什么是自动装配? ​ 自动装配就是会通过Spring 的上下文为你找出相应依赖项的类,通俗的说就是Spring 会在上下文中自动查找,并自动给Bean装配与其相关的属 ...

  4. Spring 基于注解(annotation)的配置之@Autowired注解

    Setter 方法中的 @Autowired 当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解,它会试图执行 byType 自动连接.换言之,加了@Autowired ...

  5. Spring中Autowired注解到底怎么实现的

    前言 使用Spring开发时,进行配置主要有两种方式,一是XML的方式,二是Java Config的方式. Spring技术自身也在不断的发展和改变,从当前springboot的火热程度来看,java ...

  6. Spring学习笔记—最小化Spring XML配置

    自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系. 自动 ...

  7. @autowired注解原理_SpringBoot注解大全,收藏一波!!!

    一.注解(annotations)列表 二.注解(annotations)详解 三.JPA注解 四.springMVC相关注解 五.全局异常处理 <Java 2019 超神之路> < ...

  8. @autowired注解_SpringBoot常用注解大全

    作者:tanwei81 , 链接:www.cnblogs.com/tanwei81 一.注解(annotations)列表 @SpringBootApplication: 包含了@ComponentS ...

  9. 深入理解@Autowired注解

    本文来说下@Autowired注解,这个注解在平时的开发中也是使用的比较多,但是很多人可能也就是仅仅会使用而已,本文来说下其实现原理. 文章目录 @Autowired注解概述 @Autowired注解 ...

最新文章

  1. 关于json的操作 jsonArray---jsonObject
  2. xcode6.3配置svn,详情教程,小白戳进来。
  3. 计算机考试题 实操,计算机考试实操题-20210604194811.docx-原创力文档
  4. hdu4515 小模拟
  5. 程序员找工作防止小破公司的画饼充饥方法
  6. haproxy+keepalived实现高可用K8S集群部署
  7. 21南阳理工oj新生赛Round#5--这是一道二分题
  8. 拓端tecdat|R语言ROC曲线评价分类器的好坏
  9. Java开发工程师面试笔试试题,真题;
  10. Python 总体架构
  11. NOIP2013普及组初赛
  12. windows 删除服务命令
  13. 几个轻巧好用的代码检查工具!代码不在坏味道
  14. EPIVAN | 基于预训练和注意力机制的启动子增强子相互作用预测
  15. ubuntu16.04 双显卡 安装N卡驱动
  16. android 计算屏幕尺寸来区分phone和pad
  17. BUAA(2021春)查家谱(士谔书院16级期末)——找最近公共祖先(已上传测试数据和代码)
  18. 如何评估社交网络中信息内容的价值呢?
  19. 没有内容的文集,竟然是不存在的
  20. 我是一个硬盘(转自北大未名)

热门文章

  1. 水星MW300R v2 路由器刷DD-Wrt 小记
  2. Linux 应用---make及makefile的编写
  3. 2019年最流行的10个前端框架
  4. nuxt 服务器构建因太耗CPU进程被杀解决办法
  5. React开发(126):ant design学习指南之form中的自定义校验labelCol
  6. react学习(64)--简单的锚点封装
  7. 前端学习(3201):react插件
  8. react学习(15)---antdesign form两种验证
  9. [vue] 在vue中使用this应该注意哪些问题?
  10. 前端学习(2885):如何短时间内实现v-for 搭建环境 业务模块设计