spring学习19-使用限定符
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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("normal")
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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Qualifier("festival")
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.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class UserServiceTest {
@Autowired
@Qualifier("festival")
private UserService userService;
@Test
public void testAdd(){
userService.add();
}
}
运行结果
[INFO ] 2019-10-30 20:08:51,472 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 20:08:51,482 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 20:08:51,484 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 20:08:51,485 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 20:08:51,487 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 20:08:51,650 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
Refreshing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Wed Oct 30 20:08:51 CST 2019]; root of context hierarchy
[DEBUG] 2019-10-30 20:08:51,672 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 20:08:51,673 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
[DEBUG] 2019-10-30 20:08:51,707 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 20:08:51,710 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:08:51,974 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:08:51,975 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:08:51,976 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:08:52,022 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:08:52,023 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:08:52,023 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:08:52,025 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:08:52,034 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:08:52,041 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:08:52,041 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:08:52,042 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:08:52,042 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:08:52,043 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:08:52,043 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:08:52,051 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:08:52,054 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:08:52,055 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:08:52,055 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:08:52,056 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:08:52,059 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:08:52,060 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'appconfig'
[DEBUG] 2019-10-30 20:08:52,060 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'appconfig'
[DEBUG] 2019-10-30 20:08:52,061 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:08:52,069 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'appconfig'
[DEBUG] 2019-10-30 20:08:52,069 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'userServiceFestival'
[DEBUG] 2019-10-30 20:08:52,070 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'userServiceFestival'
[DEBUG] 2019-10-30 20:08:52,071 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:08:52,075 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'userServiceFestival'
[DEBUG] 2019-10-30 20:08:52,076 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'userServiceNormal'
[DEBUG] 2019-10-30 20:08:52,077 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'userServiceNormal'
[DEBUG] 2019-10-30 20:08:52,078 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:08:52,082 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'userServiceNormal'
[DEBUG] 2019-10-30 20:08:52,082 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
Creating shared instance of singleton bean 'userService.Appconfig'
[DEBUG] 2019-10-30 20:08:52,083 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean 'userService.Appconfig'
[DEBUG] 2019-10-30 20:08:52,084 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:08:52,089 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean 'userService.Appconfig'
[DEBUG] 2019-10-30 20:08:52,090 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:08:52,152 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'lifecycleProcessor'
[DEBUG] 2019-10-30 20:08:52,162 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.UserService com.geyao.demo.service.UserServiceTest.userService
[DEBUG] 2019-10-30 20:08:52,169 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'userServiceFestival'
[DEBUG] 2019-10-30 20:08:52,170 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'userServiceNormal'
[DEBUG] 2019-10-30 20:08:52,171 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 'userServiceFestival'
添加用户并发送优惠券
[INFO ] 2019-10-30 20:08:52,182 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984)
Closing org.springframework.context.support.GenericApplicationContext@4df828d7: startup date [Wed Oct 30 20:08:51 CST 2019]; root of context hierarchy
[DEBUG] 2019-10-30 20:08:52,184 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean 'lifecycleProcessor'
[DEBUG] 2019-10-30 20:08:52,184 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南