CompactDisc类
package soundSystem;
import org.springframework.stereotype.Component;
//注解@Component
public class CompactDisc {
public CompactDisc() {
super();
System.out.println("compactdisc无参构造方法");
}
public void play(){
System.out.println("正在播放音乐....");
}
}
CDplay类
package soundSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CDPlayer {
@Autowired(required = false)
private CompactDisc cd;
@Autowired
private Power power;
public CDPlayer() {
super();
System.out.println("CDPlayer无参构造方法");
}
/*@Autowired
public CDPlayer(CompactDisc cd) {
this.cd = cd;
System.out.println("CDPlayer有参构造方法");
}*/
/*
public CDPlayer(CompactDisc cd, Power power) {
this.cd = cd;
this.power = power;
System.out.println("CDplayer的参数配置");
}
*/
public void play(){
power.supply();
if(cd!=null) {
cd.play();
}
}
}
appconfig类
package soundSystem;
import org.springframework.context.annotation.ComponentScan;
//spring注解类
@ComponentScan
public class Appconfig {
}
power类
package soundSystem;
import org.springframework.stereotype.Component;
@Component
public class Power {
public Power() {
super();
}
public void supply(){
System.out.println("电源宫殿中..");
}
}
apptest类
package soundSystem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//@RunWith(SpringJunit4ClassRunner.class)
//@ContextConfiguration(class=)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class AppTest {
@Autowired
private CDPlayer player;
@Test
public void testPlay(){
player.play();
}
}
运行结果
[INFO ] 2019-10-30 18:42:32,195 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getDefaultTestExecutionListenerClassNames(AbstractTestContextBootstrapper.java:260)
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
[INFO ] 2019-10-30 18:42:32,203 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the d