spring框架应用系列一:annotation-config自动装配
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"> <!--<!–使用基于注解自动装配 @Autowired @Inject @Resource–>-->
<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自动装配的更多相关文章
- Spring框架学习笔记(5)——自动装配
1.通过bean标签的autowire属性可以实现bean属性的自动装配. 创建一个新的Spring配置文件beans-autowire.xml,这里我们配置了3个bean,Address.Car.P ...
- 模仿 spring IOC Annotation版自动装配
spring 有两大核心 IOC和AOP. IOC (inversion of control) 译为 控制反转,也可以称为 依赖注入 ; AOP(Aspect Oriented Programmi ...
- 大厂面试官问你META-INF/spring.factories要怎么实现自动扫描、自动装配?
大厂面试官问你META-INF/spring.factories要怎么实现自动扫描.自动装配? 很多程序员想面试进互联网大厂,但是也有很多人不知道进入大厂需要具备哪些条件,以及面试官会问哪些问题, ...
- 使用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...”. 原 ...
- spring框架应用系列二:component-scan自动扫描注册装配
component-scan自动扫描注册装配 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7717331.html ...
- Spring系列7:`autowire`自动装配怎么玩
回顾 前几篇我们介绍各种依赖依赖注入,都是显式指定的,配置明确但同时也有些繁杂和重复."很多发明的出发点,都是为了偷懒,懒人是推动社会进步的原动力".Spring 提供了自动注入依 ...
- 使用Spring的JavaConfig 和 @Autowired注解与自动装配
1 JavaConfig 配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...
- Spring学习记录(十一)---使用注解和自动装配
Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入c ...
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
随机推荐
- 201521123007《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.1有关继承的知识点: 1.2有关多态 多态性:相同的形态,不同的行为.体现在相同的方法名 ...
- 201521123110 《JAVA程序设计》第3周学习总结
1.本章学习总结 ` ` 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...
- 201521123103 《Java学习笔记》第二周学习笔记
一.本周学习总结 1.学习了数据类型的使用:整数类型.浮点类型. boolean类型.数组等以及类型的转换,最重要的是学会了import引用包: 2.学习了string类对象的拼接.字符串池.枚举类型 ...
- 201521123015《Java程序设计》第1周学习总结
1.本周学习总结 知道了JAVA语言的发展历史和目前使用的版本,还有什么是JDK(Java Development Kit).JRE (Java Runtime Environment).JVM(Ja ...
- 201521123068 《java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...
- Ansible系列(一):基本配置和使用
本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...
- lintcode.46 主元素
给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. 注意事项 You may assume that the array is non-empty and the ma ...
- PHP 动态调整内存限制
最近公司的一个PHP项目在操作大文件的时候总是抛出这个异常 Fixing PHP Fatal Error: Allowed Memory Size Exhausted 经过一番调试后发现是达到了PHP ...
- 将eclipse左边目录结构改为 树形结构
认情况下Eclipse的显示内容如下: 这种视图我们看起来不太方便,于是我们需要改变一下它的显示结构,我们点击一下下图中蓝色圆圈圈住的图标 在弹出的框中把鼠标放到"package prese ...
- Redis常见的应用场景解析
Redis是一个key-value存储系统,现在在各种系统中的使用越来越多,大部分情况下是因为其高性能的特性,被当做缓存使用,这里介绍下Redis经常遇到的使用场景. Redis特性 一个产品的使用场 ...