我想基於使用Spring戰略模式來實現通信服務功能。我有以下類 -NoUniqueBeanDefinitionException - 春DI與策略模式
接口 - MessageService.java
package com.xxx
public Interface MessageService{
String sendMessage(String idOrNumber);
}
實現類 -
1)EmailService.java
package com.xxx
@Component
public class EmailService implements MessageService{
public String sendMessage(String idOrNumber){
// Do some operation
return "success"
}
}
2)SmsService.java
package com.xxx
@Component
public class SmsService implements MessageService{
public String sendMessage(String idOrNumber){
// Do some operation
return "success"
}
}
CommunicationFactory類
package com.xxx
@Component
public class CommunicationFactory {
@Resource(name ="smsService")
private SmsService smsService
@Resource(name ="emailService")
private EmailService emailService;
public MessageService getCommunicationChannel(String channel){
MessageService messageService = null;
if("sms".equals(channel){
messageService = smsService;
}
if("email".equals(channel){
messageService = emailService;
}
return messageService;
}
我有一個郵件服務的實現類
package com.xxx
@Component
@Service
public class CommunicationServiceImpl implements CommunicationService {
@Autowired
private MessageService messageService;
CommunicationFactory communicationFactory;
@Override
public String sendCommunication(String idOrNumber){
//Which implementation be called - SMS or EMAIL
messageService = communicationFactory.getCommunicationChannel(channel);
String statusMessage = messageService.sendMessage(idOrNumber);
}
}
我收到以下錯誤運行的服務器,而。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xxx.MessageService com.xxx.CommunicationServiceImpl.messageService; nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.xxx.MessageService] is defined: expected single matching bean but found 2: smsService,emailService
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) [spring-beans-
3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-
3.2.3.RELEASE.jar:3.2.3.RELEASE]
... 25 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.xxx.MessageService] is defined: expected single matching bean but found 2:
smsService,emailService
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:863) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768) [spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) [spring-beans-
3.2.3.RELEASE.jar:3.2.3.RELEASE]
... 27 more
我在哪裏出錯了?任何指針將是有益的
你必須在服務類中沒有限定。 – chrylis 2014-09-01 22:28:38