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 default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] [INFO ] 2019-10-30 18:42:32,205 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209) Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] [INFO ] 2019-10-30 18:42:32,206 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209) Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] [INFO ] 2019-10-30 18:42:32,208 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getTestExecutionListeners(AbstractTestContextBootstrapper.java:187) Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1786f9d5, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@704d6e83, org.springframework.test.context.support.DirtiesContextTestExecutionListener@43a0cee9][INFO ] 2019-10-30 18:42:32,393 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583) Refreshing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Wed Oct 30 18:42:32 CST 2019]; root of context hierarchy [DEBUG] 2019-10-30 18:42:32,427 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,427 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,454 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,458 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,557 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,557 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,559 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,590 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,591 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,591 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,593 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,599 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,606 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@158da8e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,CDPlayer,power]; root of factory hierarchy [DEBUG] 2019-10-30 18:42:32,606 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,606 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,607 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-30 18:42:32,607 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' [DEBUG] 2019-10-30 18:42:32,607 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor' [DEBUG] 2019-10-30 18:42:32,613 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,618 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor' [DEBUG] 2019-10-30 18:42:32,619 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-30 18:42:32,619 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-30 18:42:32,620 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,623 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-30 18:42:32,624 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'appconfig' [DEBUG] 2019-10-30 18:42:32,625 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'appconfig' [DEBUG] 2019-10-30 18:42:32,626 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'appconfig' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,628 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'appconfig' [DEBUG] 2019-10-30 18:42:32,629 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'CDPlayer' [DEBUG] 2019-10-30 18:42:32,629 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'CDPlayer' CDPlayer无参构造方法 [DEBUG] 2019-10-30 18:42:32,634 method:org.springframework.beans.factory.annotation.InjectionMetadata.checkConfigMembers(InjectionMetadata.java:72) Registered injected element on class [soundSystem.CDPlayer]: AutowiredFieldElement for private soundSystem.CompactDisc soundSystem.CDPlayer.cd [DEBUG] 2019-10-30 18:42:32,634 method:org.springframework.beans.factory.annotation.InjectionMetadata.checkConfigMembers(InjectionMetadata.java:72) Registered injected element on class [soundSystem.CDPlayer]: AutowiredFieldElement for private soundSystem.Power soundSystem.CDPlayer.power [DEBUG] 2019-10-30 18:42:32,634 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'CDPlayer' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,638 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86) Processing injected element of bean 'CDPlayer': AutowiredFieldElement for private soundSystem.CompactDisc soundSystem.CDPlayer.cd [DEBUG] 2019-10-30 18:42:32,648 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86) Processing injected element of bean 'CDPlayer': AutowiredFieldElement for private soundSystem.Power soundSystem.CDPlayer.power [DEBUG] 2019-10-30 18:42:32,653 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'power' [DEBUG] 2019-10-30 18:42:32,654 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'power' [DEBUG] 2019-10-30 18:42:32,655 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'power' to allow for resolving potential circular references [DEBUG] 2019-10-30 18:42:32,658 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'power' [DEBUG] 2019-10-30 18:42:32,658 method:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans(AutowiredAnnotationBeanPostProcessor.java:535) Autowiring by type from bean name 'CDPlayer' to bean named 'power' [DEBUG] 2019-10-30 18:42:32,660 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'CDPlayer' [DEBUG] 2019-10-30 18:42:32,661 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'power' [DEBUG] 2019-10-30 18:42:32,662 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-30 18:42:32,762 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'lifecycleProcessor' [DEBUG] 2019-10-30 18:42:32,772 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86) Processing injected element of bean 'soundSystem.AppTest': AutowiredFieldElement for private soundSystem.CDPlayer soundSystem.AppTest.player [DEBUG] 2019-10-30 18:42:32,773 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'CDPlayer' [DEBUG] 2019-10-30 18:42:32,774 method:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans(AutowiredAnnotationBeanPostProcessor.java:535) Autowiring by type from bean name 'soundSystem.AppTest' to bean named 'CDPlayer' 电源宫殿中.. [INFO ] 2019-10-30 18:42:32,783 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984) Closing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Wed Oct 30 18:42:32 CST 2019]; root of context hierarchy [DEBUG] 2019-10-30 18:42:32,785 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'lifecycleProcessor' [DEBUG] 2019-10-30 18:42:32,786 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@158da8e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,CDPlayer,power]; root of factory hierarchy