annotation-config自动装配

本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7716678.html

解决问题

通过spring XML配置文件,实现类对象之间松耦合

前提条件

使用的bean必须在spring容器中已注册过

内容说明

1、 使用@Autowired注解自动装配时,需在XML配置文件中引入 <context:annotation-config/>;

2、 存在多个bean满足装配属性,需用@Qualifier指定唯一的bean,否则会报异常;

Guitar和Saxophone都实现了Instrument接口,Instrumentalist存在一个属性类型为Instrument,

所以Instrumentalist满足装配Instrument属性的bean就有 Guitar和Saxophone,所以必须指定唯一的bean;

3、 bean 的id不指定时,默认类名小写;

例如<bean class="com.spring.example.annotation.config.Guitar"/> 因为id没有指明,所以默认为guitar;

应用实例(包名:com.spring.example.annotation.config)

spring配置文件 annotation-config.xml 如下:

 <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--&lt;!&ndash;使用基于注解自动装配 @Autowired @Inject @Resource&ndash;&gt;-->
<context:annotation-config/> <!--限定歧义性的依赖,使用@Autowired注解自动装配,满足装配的多个bean,
可以通过@Qualifier指定来缩小范围 默认byType
Guitar和Saxophone都实现了Instrument,所以有多个bean满足Instrumentalist注入属性,
需要指定唯一bean,用@Qualifier("guitar")指定,否则会报异常
--> <bean id ="guitar" class="com.spring.example.annotation.config.Guitar"/>
<bean id ="saxophone" class="com.spring.example.annotation.config.Saxophone"/>
<bean id ="kenny"
class="com.spring.example.annotation.config.Instrumentalist">
<property name="song" value="Jingle Bells3" />
</bean> </beans>

Instrument接口代码

public interface Instrument {
void play();
}

Guitar实现接口Instrument代码

public class Guitar implements Instrument {
@Override
public void play() {
System.out.println("Guitar....");
}
}

Saxophone实现接口Instrument代码

public class Saxophone implements Instrument {
@Override
public void play() {
System.out.println("Saxophone ......");
}
}

Performer接口代码

public interface Performer {

    void perform();
}

Instrumentalist实现接口Performer代码

public class Instrumentalist implements Performer {

    public Instrumentalist(){}
@Value("Yesterday Once more !") //song 初始化值
private String song; // @Autowired 可以装配属性、方法、构造函数,只要类型相同(这里是Instrument类型)
@Autowired
@Qualifier("guitar") //spring容器中有多个bean满足要求,需要指定bean
private Instrument instrument; public void setSong(String song) {
this.song = song;
} public void setInstrument(Instrument instrument){
this.instrument = instrument ;
} @Override
public void perform() {
System.out.println("Playing "+ song + " : ");
instrument.play();
} }

测试代码

public class Driver extends Application {

    public static void main(String[] args) {
launch(args);
} @Override
public void start(Stage primaryStage) {
try { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/annotation-config.xml");
Performer performer = (Performer) ctx.getBean("kenny");
performer.perform(); }catch (Exception e){
e.printStackTrace();
}
}
}

运行结果

总结

annotation-config下@Autowired自动装配自然也有其优缺点:

优点:实现类对象之间松耦合,

缺点:需要在spring配置文件中需提前手动指明bean,增加XML文件繁琐冗余性;

在业务逻辑开发中不能将业务类自动注入spring容器,遇到业务类的更改,配置文件也不得不更改,增加出错概率;

component-scan能解决此问题,会在下节讲述;

应用场景

调用第三方接口,装配注入相应实例bean;

本文描述可能有不对或不全之处,欢迎大家吐槽!

不要让懒惰占据你的大脑,不要让妥协拖垮你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下。

spring框架应用系列一:annotation-config自动装配的更多相关文章

  1. Spring框架学习笔记(5)——自动装配

    1.通过bean标签的autowire属性可以实现bean属性的自动装配. 创建一个新的Spring配置文件beans-autowire.xml,这里我们配置了3个bean,Address.Car.P ...

  2. 模仿 spring IOC Annotation版自动装配

    spring 有两大核心 IOC和AOP.  IOC (inversion of control) 译为 控制反转,也可以称为 依赖注入 ; AOP(Aspect Oriented Programmi ...

  3. 大厂面试官问你META-INF/spring.factories要怎么实现自动扫描、自动装配?

    大厂面试官问你META-INF/spring.factories要怎么实现自动扫描.自动装配?   很多程序员想面试进互联网大厂,但是也有很多人不知道进入大厂需要具备哪些条件,以及面试官会问哪些问题, ...

  4. 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”

    使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...

  5. spring框架应用系列二:component-scan自动扫描注册装配

    component-scan自动扫描注册装配 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7717331.html ...

  6. Spring系列7:`autowire`自动装配怎么玩

    回顾 前几篇我们介绍各种依赖依赖注入,都是显式指定的,配置明确但同时也有些繁杂和重复."很多发明的出发点,都是为了偷懒,懒人是推动社会进步的原动力".Spring 提供了自动注入依 ...

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

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

  8. Spring学习记录(十一)---使用注解和自动装配

    Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入c ...

  9. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

随机推荐

  1. 201521123034《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...

  2. Java课程设计——计算器团队博客

    1.团队名称.团队成员介绍(需要有照片) 1.1团队名称 707 1.2团队成员介绍 谢元将:组长 罗登宇:组员 王华俊:组员 2. 项目git地址 谢元将 罗登宇 王华俊 3. 项目git提交记录截 ...

  3. Excel表科学记数法的数字和文本的转换

    一,科学记数法的数字转换文本类型: 1,还未有数据,先选中列或者单元格 右键单击->设置单元格格式->文本->确定 2,已有数据,先选中列或者单元格 右键单击->设置单元格格式 ...

  4. 在centos6,7 上编译安装内核

      小编以前写过一篇软件的源码编译安装,今天小编再给大家带来一篇内核的编译安装.   今天,就以centos7 编译安装最新版本4.13.2 内核为例,给大家详解.编译安装之前,检查一下自己的磁盘空间 ...

  5. MongoDB 分布式架构 复制 分片 适用性范围

    转载自 http://www.mongoing.com/archives/3573

  6. eclipse Maven新建一个项目并使用

      安装参考这篇博文eclipse配置maven + 创建maven项目(三)  打开pom.xml 试着添加MySQL的JDBC驱动 添加如下配置, <dependency> <g ...

  7. 图文详解在Windows server 2008 R2上安装SQL Server 2012集群

    1.准备: 4台服务器(1台AD.2台SQL服务器.1台iSCSI存储服务器) 9个IP(1个AD的IP.2个SQL服务器的IP.2个心跳IP.1个iSCSI存储服务器的IP.1个集群IP.1个DTC ...

  8. String类的一些常见的获取方法(5)

     String s = "aasfasfdtgsrast"; 1: int a = s.length() //返回字符串的长度 2: char s1 = charAt(int in ...

  9. MVC查询数据接收及校验

    本来想写一篇aspx的TreeView控件绑值的文章的,在写案例的时候,写了一半,发现有些地方还得考虑以下,就留待下次了. 这一篇的话,是最近在开发一个项目的时候,有大量的页面和数据表,需要花式查询, ...

  10. webpack2系列step1

    第一篇:HTML 本文将一步一步的介绍webpack2的配置,从最基础的一直到与node结合. 操作都一样: midir step1 && cd step1 npm init -y n ...