首先是自动配置部分
CD抽象类和AvrilCD实现类,使用自动配置
public interface CD {
void play();
}
public class AvrilCD implements CD {
public void play() {
System.out.println("AvrilCD");
}
}
配置类:
@Configuration//作为配置文件之一
@ComponentScan
public class CDPlayerConfig {
}
第二种是 xml 配置
实现 CD 接口的 BlankDisc 类,不使用@Component 标注
public class BlankDisc implements CD {
private String title;
private String artist;
private List<String> tracks;
public BlankDisc(String title, String artist, List<String> tracks) {
this.title = title;
this.artist = artist;
this.tracks = tracks;
}
public void play() {
System.out.println("playing " + title + " by " + artist);
for(String track: tracks){
System.out.println("-track:" + track);
}
}
}
MediaPlayer接口,和其实现类CDPlayer
public interface MediaPlayer {
void playCD();
}
public class CDPlayer implements MediaPlayer {
public CD cd;
//@Autowired//可以用在构造方法和方法中
public CDPlayer(CD cd) {
this.cd = cd;
}
public void playCD(){
cd.play();
}
}
混合配置类,导入前面使用扫描的配置类
/**
* 导入和混合配置
*/
@Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:JavaConfig.xml")
public class SoundsystemConfig {
}
xml文件放在 classpath 路径(main/resources/JavaConfig.xml),构造方法配置bean,不是setter方式
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
<bean class="com.qhf.soundsystem04.AvrilCD" id="avrilCD"></bean>
-->
<!--两种配置方案-->
<!--1.constructor-arg方法,构造方法传参,不是setter传参
<bean class="com.qhf.soundsystem03.CDPlayer" id="cdPlayer">
<constructor-arg ref="CD"></constructor-arg>
</bean>
-->
<!--2.c-命名空间:cd为构造器参数名,-ref:注入bean引用声明,=“CD”:要注入的bean的id-->
<bean class="com.qhf.soundsystem04.CDPlayer" id="cdPlayer" c:cd-ref="blankCD"></bean>
<!-- 无集合的情况下,c-标签比较方便
<bean id="blankCD" class="com.qhf.soundsystem04.BlankDisc"
c:title="qhfcd" c:artist="qhf"></bean>
-->
<!-- 有集合的情况下,constructor-arg 标签比较方便-->
<bean id="blankCD" class="com.qhf.soundsystem04.BlankDisc">
<constructor-arg index="0" value="qhfcd"/>
<constructor-arg index="1" value="qhf"/>
<constructor-arg index="2" >
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</constructor-arg>
</bean>
</beans>
测试类:
/**
* 使用xml配置文件
* 首先把xml配置文件放在classpath位置,不能是java源文件位置下
* 然后在CDPlayerConfig 类添加标注@ImportResource("classpath:JavaConfig.xml")
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SoundsystemConfig.class)
public class CDPlayerTest {
@Autowired
private AvrilCD avrilCD;
@Autowired
private BlankDisc blankDisc;
@Autowired
private CDPlayer cdp;
//@Autowired
//private CD cd;//报错,因为有两个bean:avrilCD 和blankCD 都实现了CD接口,自动注入无法选择
@Test
public void cdShouldNotBeNull(){
assertNotNull(avrilCD);
assertNotNull(blankDisc);
assertNotNull(cdp);
avrilCD.play();
blankDisc.play();
cdp.playCD();
}
}
结果:
AvrilCD
playing qhfcd by qhf
-track:1
-track:2
-track:3
playing qhfcd by qhf
-track:1
-track:2
-track:3