Instruments.java 创建乐器接口
package spring.ioc04;
public interface Instruments {
public void play();
}
Cello.java 大提琴
package spring.ioc04;
public class Cello implements Instruments {
@Override
public void play() {
System.out.print("大提琴 ");
}
}
Piano.java 钢琴
package spring.ioc04;
public class Piano implements Instruments {
@Override
public void play() {
System.out.print("钢琴 ");
}
}
Violin.java 小提琴
package spring.ioc04;
public class Violin implements Instruments {
@Override
public void play() {
System.out.print("小提琴 ");
}
}
上面是这次例子要用到的实体类,我们先看下xml配置和单元测试类,知道我们要先干什么。
ioc04.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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<!-- 告诉spring我们要用基于注解的自动装配 -->
<context:annotation-config></context:annotation-config>
<bean id="cello" class="spring.ioc04.Cello"></bean>
<bean id="piano" class="spring.ioc04.Piano">
</bean>
<bean id="pan" class="spring.ioc04.Pan">
</bean>
</beans>
单元测试类:
/**
* spring.ioc04 autowire减少xml配置
*/
@Test
public void test6() {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc04.xml");
spring.ioc04.Pan pan = (spring.ioc04.Pan) context.getBean("pan");
pan.performance();
}
我们读取ioc04.xml,然后拿到pan的实例,xml文件中context:annotation-config标签是告诉spring我们要用注解装配对象。(这次的注解装配并不完全,乐器类和pan类仍使用bean标签装配,因为我们这次重点介绍autowire注解。),我们看一下pan类:
package spring.ioc04;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Pan {
//默认@Autowired是必须有装配对象的,没有的话会报错,如果想远离失败,null也可以接受的话,可以设定required=false
@Autowired(required=false)
//根据xml中的bean ID查找,缩小自动装配的范围
@Qualifier("piano")
private Instruments instrument;
public void performance() {
System.out.print("我会 ");
instrument.play();
}
public Instruments getInstrument() {
return instrument;
}
public void setInstrument(Instruments instrument) {
this.instrument = instrument;
}
}
@Autowired标签会根据属性名 在spring上下文中找相同id名的实例,@Autowired会生效也是因为context:annotation-config标签的使用,然后下面又有一个@Qualifier(“piano”)标签,因为我配置了三种乐器,而instrument属性是一个乐器接口,spring不知道用什么哪个乐器去装配,导致抛出异常,所以用@Qualifier(“piano”)标签去限定用哪一种乐器,piano表示我们用钢琴来装配。最后执行performance()输出结果为:
我会 钢琴
本例中 我们因为使用autowire注解,从而在xml文件中,我们不需要为bean id为pan的实体添加property标签或constructor-arg标签,从而达到减少xml配置的作用。