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 { private CompactDisc cd; public CDPlayer() { super(); System.out.println("CDPlayer无参构造方法"); } @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; System.out.println("CDPlayer有参构造方法"); } public void play(){ cd.play(); } }App类 package soundSystem; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; public class App { public static void main(String[] args){ ApplicationContext context=new AnnotationConfigApplicationContext(App.class); CDPlayer player=context.getBean(CDPlayer.class); player.play(); } }appconfig类 package soundSystem; import org.springframework.context.annotation.ComponentScan; //spring注解类 @ComponentScan public class Appconfig { }运行结果 [INFO ] 2019-10-29 19:11:30,833 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583) Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@880ec60: startup date [Tue Oct 29 19:11:30 CST 2019]; root of context hierarchy [DEBUG] 2019-10-29 19:11:30,857 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-29 19:11:30,857 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-29 19:11:30,883 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-29 19:11:30,887 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' [DEBUG] 2019-10-29 19:11:30,965 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-29 19:11:30,965 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-29 19:11:30,966 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-29 19:11:30,998 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' [DEBUG] 2019-10-29 19:11:30,998 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-29 19:11:30,998 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-29 19:11:31,000 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-29 19:11:31,004 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' [DEBUG] 2019-10-29 19:11:31,009 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@40f08448: 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,compactDisc]; root of factory hierarchy [DEBUG] 2019-10-29 19:11:31,009 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-29 19:11:31,010 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-29 19:11:31,012 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-29 19:11:31,012 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-29 19:11:31,012 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor' [DEBUG] 2019-10-29 19:11:31,019 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-29 19:11:31,023 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor' [DEBUG] 2019-10-29 19:11:31,023 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-29 19:11:31,023 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-29 19:11:31,024 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-29 19:11:31,028 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory' [DEBUG] 2019-10-29 19:11:31,029 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'appconfig' [DEBUG] 2019-10-29 19:11:31,029 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'appconfig' [DEBUG] 2019-10-29 19:11:31,030 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-29 19:11:31,032 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'appconfig' [DEBUG] 2019-10-29 19:11:31,033 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'CDPlayer' [DEBUG] 2019-10-29 19:11:31,033 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'CDPlayer' [DEBUG] 2019-10-29 19:11:31,048 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221) Creating shared instance of singleton bean 'compactDisc' [DEBUG] 2019-10-29 19:11:31,049 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449) Creating instance of bean 'compactDisc' compactdisc无参构造方法 [DEBUG] 2019-10-29 19:11:31,050 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) Eagerly caching bean 'compactDisc' to allow for resolving potential circular references [DEBUG] 2019-10-29 19:11:31,054 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'compactDisc' [DEBUG] 2019-10-29 19:11:31,055 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757) Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc' CDPlayer有参构造方法 [DEBUG] 2019-10-29 19:11:31,057 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-29 19:11:31,058 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485) Finished creating instance of bean 'CDPlayer' [DEBUG] 2019-10-29 19:11:31,059 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'compactDisc' [DEBUG] 2019-10-29 19:11:31,059 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-29 19:11:31,120 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'lifecycleProcessor' [DEBUG] 2019-10-29 19:11:31,127 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) Returning cached instance of singleton bean 'CDPlayer' 正在播放音乐....
