spring学习17-自动装配的歧义性

userService类

    package com.geyao.demo.service;
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Appconfig.class)
    public class UserServiceTest {
        @Autowired
        private UserService userService;
        @Test
        public void  testAdd(){
            userService.add();
        }
    }

UserServiceNormal类

    package com.geyao.demo.service.com.geyao.demo.service.impl;
     
    import com.geyao.demo.service.UserService;
    import org.springframework.stereotype.Component;
     
    @Component
    public class UserServiceNormal implements UserService {
     
        public void add(){
            System.out.println("添加用户");
        }
     
    }

Appconfig类

    package com.geyao.demo.service;
     
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
     
    @Configuration
    @ComponentScan
    public class Appconfig {
    }

userserviceFestival类

    package com.geyao.demo.service.com.geyao.demo.service.impl;
     
    import com.geyao.demo.service.UserService;
    import org.springframework.stereotype.Component;
     
    @Component
    public class UserServiceFestival implements UserService {
        public void add(){
            System.out.println("添加用户并发送优惠券");
        }
    }

UserServiceTest类

    package com.geyao.demo.service;
     
    import com.geyao.demo.service.com.geyao.demo.service.impl.UserServiceNormal;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Appconfig.class)
    public class UserServiceTest {
        @Autowired
    //歧义性解决,改成对应的类
       private UserServiceNormal userService;
     
        @Test
        public void  testAdd(){
            userService.add();
        }
    }

 运行结果

    [DEBUG] 2019-10-30 20:00:15,814 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/geyao/.m2/repository/org/springframework/spring-core/4.3.13.RELEASE/spring-core-4.3.13.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
    WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
    [DEBUG] 2019-10-30 20:00:16,067 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 20:00:16,067 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-30 20:00:16,068 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 20:00:16,117 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 20:00:16,117 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 20:00:16,117 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-30 20:00:16,119 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 20:00:16,126 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 20:00:16,134 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
    Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@44ebcd03: 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,userServiceFestival,userServiceNormal,userService.Appconfig]; root of factory hierarchy
    [DEBUG] 2019-10-30 20:00:16,135 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 20:00:16,135 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 20:00:16,136 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 20:00:16,136 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 20:00:16,136 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
    [DEBUG] 2019-10-30 20:00:16,141 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 20:00:16,146 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 20:00:16,147 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 20:00:16,147 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-30 20:00:16,148 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 20:00:16,151 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 20:00:16,152 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'appconfig'
    [DEBUG] 2019-10-30 20:00:16,152 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'appconfig'
    [DEBUG] 2019-10-30 20:00:16,153 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 20:00:16,157 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'appconfig'
    [DEBUG] 2019-10-30 20:00:16,158 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'userServiceFestival'
    [DEBUG] 2019-10-30 20:00:16,158 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'userServiceFestival'
    [DEBUG] 2019-10-30 20:00:16,159 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'userServiceFestival' to allow for resolving potential circular references
    [DEBUG] 2019-10-30 20:00:16,163 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'userServiceFestival'
    [DEBUG] 2019-10-30 20:00:16,164 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'userServiceNormal'
    [DEBUG] 2019-10-30 20:00:16,164 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'userServiceNormal'
    [DEBUG] 2019-10-30 20:00:16,166 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'userServiceNormal' to allow for resolving potential circular references
    [DEBUG] 2019-10-30 20:00:16,169 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'userServiceNormal'
    [DEBUG] 2019-10-30 20:00:16,169 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'userService.Appconfig'
    [DEBUG] 2019-10-30 20:00:16,170 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'userService.Appconfig'
    [DEBUG] 2019-10-30 20:00:16,172 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'userService.Appconfig' to allow for resolving potential circular references
    [DEBUG] 2019-10-30 20:00:16,178 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'userService.Appconfig'
    [DEBUG] 2019-10-30 20:00:16,178 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 20:00:16,247 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'lifecycleProcessor'
    [DEBUG] 2019-10-30 20:00:16,258 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86)
    Processing injected element of bean 'com.geyao.demo.service.UserServiceTest': AutowiredFieldElement for private com.geyao.demo.service.com.geyao.demo.service.impl.UserServiceNormal com.geyao.demo.service.UserServiceTest.userService
    [DEBUG] 2019-10-30 20:00:16,267 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'userServiceNormal'
    [DEBUG] 2019-10-30 20:00:16,268 method:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans(AutowiredAnnotationBeanPostProcessor.java:535)
    Autowiring by type from bean name 'com.geyao.demo.service.UserServiceTest' to bean named 'userServiceNormal'
    添加用户
    [INFO ] 2019-10-30 20:00:16,277 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984)
    Closing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Wed Oct 30 20:00:15 CST 2019]; root of context hierarchy
    [DEBUG] 2019-10-30 20:00:16,278 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'lifecycleProcessor'
    [DEBUG] 2019-10-30 20:00:16,279 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512)
    Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@44ebcd03: 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,userServiceFestival,userServiceNormal,userService.Appconfig]; root of factory hierarchy
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值