Я пытаюсь реализовать функциональность службы связи на основе шаблона стратегии с использованием Spring. У меня есть следующие классы -NoUniqueBeanDefinitionException - Spring 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