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

父类

package cn.ychx;

public interface Person {

	public void sayHello();

}

学生子类

package cn.ychx;

public class Student implements Person {

	@Override
public void sayHello() {
System.out.println("Hello! My name is Tom, I'm a Student.");
} }

教师子类

package cn.ychx;

public class Teacher implements Person {

	@Override
public 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; @Override
public 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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://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的特定依赖

Spring自动装配----注解装配----Spring自带的@Autowired注解的更多相关文章

  1. Spring 自动转配类 在类中使用@Bean 注解进行转配但是需要排除该类说明

    在spring中可以使用 @Component @Configuration @Bean(实例化后返回该bean)进行类实例的自动装配. 需求: 排除指定需要自动转配的类. 说明: 1.在以上注解中  ...

  2. Spring温故而知新 - bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  3. Spring温故而知新 – bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  4. Spring重温(四)--Spring自动组件扫描

    通常情况下,声明所有的Bean类或组件的XML bean配置文件,这样Spring容器可以检测并注册Bean类或组件. 其实,Spring是能够自动扫描,检测和预定义的项目包并实例化bean,不再有繁 ...

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

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

  6. 理解Spring(一):Spring 与 IoC

    目录 什么是 Spring Spring 的整体架构 什么是 IoC Bean 的概念 Spring 的基本使用 Spring 的两种 IoC 容器 Spring 容器的基本工作原理 Spring B ...

  7. Spring自动装配与扫描注解

    1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...

  8. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  9. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

随机推荐

  1. 插入订单并且输出订单号的sql存储过程

    --插入订单-- create proc InsertOrders ( @OrderNumber varchar(300), @OrderState varchar(30), @OrderType v ...

  2. 领扣(LeetCode)转置矩阵 个人题解

    给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7] ...

  3. Flex调用本地文件分析

    最近在用Flex做一个相册的功能,因为图片数据很多,所以想调用本地文件的方式做. 但是B/S的缘故,很多安全上的限制给我造成了不小的麻烦,把我这个小菜鸟弄的晕头转向. 第一,刚开始,查了很多资料发现都 ...

  4. tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测

    由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...

  5. 运用python实现冒泡排序算法

    冒泡排序,一个经典的排序算法,因在算法运行中,极值会像水底的气泡一样逐渐冒出来,因此而得名. 冒泡排序的过程是比较两个相邻元素的大小,然后根据大小交换位置,这样从列表左端开始冒泡,最后最大值会依次从右 ...

  6. 【Luogu P2002&P2341】消息扩散/受欢迎的奶牛

    Luogu P2002 Luogu P2341 使用强连通分量算法缩点 第一题统计入度为0的个数强连通分量数. 第二题的答案为当且仅当仅有一个强连通分量的出度为0时该强连通分量的节点数,原因如下:若一 ...

  7. Eclipse设置Working Set管理项目和detach合并分离窗口

    当项目多了的时候,使用Working Set分组管理项目很有必要了,不然一大推项目在一起 找起来麻烦,看起来也难受~ ​ 所以根据给项目不同分类就很有必要了. 之前myeclipse设置了,今天装了一 ...

  8. mybatis精讲(四)--ObjectFactory

    目录 前言 mybatis的ObjectFactory 源码 setProperties create instantiateClass 使用场景 # 加入战队 微信公众号 前言 ObjectFact ...

  9. Python爬虫之Selenium的常用方法

    1.单个元素的选取 find_element_by_id      通过标签属性Id查找元素 find_element_by_name      通过标签属性name查找元素 find_element ...

  10. flanneld 安装

    目录 flanneld 安装 下载分发flanneld二进制文件 分发二进制文件到所有集群的节点 创建Flannel证书和私钥 创建证书签名请求 生成证书和私钥 向etcd写入Pod网段信息 创建fl ...