spring学习14-Autowired的使用场景

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;
        private Power power;
        public CDPlayer() {
            super();
            System.out.println("CDPlayer无参构造方法");
        }
    /*@Autowired
        public CDPlayer(CompactDisc cd) {
            this.cd = cd;
            System.out.println("CDPlayer有参构造方法");
        }*/
    @Autowired
        public CDPlayer(CompactDisc cd, Power power) {
            this.cd = cd;
            this.power = power;
            System.out.println("CDplayer的参数配置");
        }
     
        public void play(){
            power.supply();
            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-29 23:36:32,969 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-29 23:36:32,976 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-29 23:36:32,977 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-29 23:36:32,978 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-29 23:36:32,979 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-29 23:36:33,076 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
    Refreshing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Tue Oct 29 23:36:33 CST 2019]; root of context hierarchy
    [DEBUG] 2019-10-29 23:36:33,086 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 23:36:33,086 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    [DEBUG] 2019-10-29 23:36:33,105 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 23:36:33,108 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 23:36:33,173 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 23:36:33,173 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-29 23:36:33,175 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 23:36:33,195 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 23:36:33,195 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 23:36:33,195 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-29 23:36:33,197 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 23:36:33,202 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 23:36:33,210 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,compactDisc,power]; root of factory hierarchy
    [DEBUG] 2019-10-29 23:36:33,210 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 23:36:33,210 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 23:36:33,210 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 23:36:33,211 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 23:36:33,211 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
    [DEBUG] 2019-10-29 23:36:33,214 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 23:36:33,217 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 23:36:33,217 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 23:36:33,218 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-29 23:36:33,218 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 23:36:33,225 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 23:36:33,226 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'appconfig'
    [DEBUG] 2019-10-29 23:36:33,226 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'appconfig'
    [DEBUG] 2019-10-29 23:36:33,227 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 23:36:33,229 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'appconfig'
    [DEBUG] 2019-10-29 23:36:33,229 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'CDPlayer'
    [DEBUG] 2019-10-29 23:36:33,230 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'CDPlayer'
    [DEBUG] 2019-10-29 23:36:33,243 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'compactDisc'
    [DEBUG] 2019-10-29 23:36:33,244 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'compactDisc'
    compactdisc无参构造方法
    [DEBUG] 2019-10-29 23:36:33,245 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 23:36:33,248 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'compactDisc'
    [DEBUG] 2019-10-29 23:36:33,251 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'power'
    [DEBUG] 2019-10-29 23:36:33,252 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'power'
    [DEBUG] 2019-10-29 23:36:33,255 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-29 23:36:33,258 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'power'
    [DEBUG] 2019-10-29 23:36:33,259 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
    Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc'
    [DEBUG] 2019-10-29 23:36:33,260 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
    Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'power'
    CDplayer的参数配置
    [DEBUG] 2019-10-29 23:36:33,262 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 23:36:33,264 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'CDPlayer'
    [DEBUG] 2019-10-29 23:36:33,264 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'compactDisc'
    [DEBUG] 2019-10-29 23:36:33,264 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'power'
    [DEBUG] 2019-10-29 23:36:33,265 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 23:36:33,329 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'lifecycleProcessor'
    [DEBUG] 2019-10-29 23:36:33,338 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-29 23:36:33,339 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'CDPlayer'
    [DEBUG] 2019-10-29 23:36:33,339 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-29 23:36:33,346 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984)
    Closing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Tue Oct 29 23:36:33 CST 2019]; root of context hierarchy
    [DEBUG] 2019-10-29 23:36:33,347 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'lifecycleProcessor'
    [DEBUG] 2019-10-29 23:36:33,347 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512)
    Destroying singletons in org.springframework.beans.factory.support.DefaultListableB
 

posted @   前端导师歌谣  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示