我正在尝试实现一个Spring电子邮件服务,为此我实现了GOOF设计模式。我创建了一个接口电子邮件服务,如下所示。
public interface EmailService {
public void sendFeedbackEmail(FeedbackPojo feedbackPojo);
public void sendGenericEmailMessage(SimpleMailMessage message);
}
然后我创建了一个抽象类,它扩展了这个接口并实现了它的一个重写方法,即sendFeedbackEmail(FeedBackPojo feedbackpojo)
这个抽象类的实现如下。
public abstract class AbstractEmailService implements EmailService {
@Value("@{default.to.address}")
private String defaultToAddress ;
protected SimpleMailMessage preparedSimpleMailMessageFromFeedbackPojo(FeedbackPojo feedback){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(defaultToAddress);
message.setFrom(feedback.getEmail());
message.setSubject("[Subscriber]: Feedback Received from"+ feedback.getFirstName()+" "+feedback.getLastName()+"!");
message.setText(feedback.getFeedback());
return message;
}
@Override
public void sendFeedbackEmail(FeedbackPojo feedbackPojo) {
sendGenericEmailMessage(preparedSimpleMailMessageFromFeedbackPojo(feedbackPojo));
}
}
然后我创建了一个MockEmailService类,它实现了EmailService接口的第二个方法,即sendGenericEmail(SimpleMailMessage message),如下所示
公共类MockEmailService扩展了AbstractEmailService {
private static final Logger log =
LoggerFactory.getLogger(MockEmailService.class);
@Override
public void sendGenericEmailMessage(SimpleMailMessage message) {
System.out.println(message.toString());
}
}
然后我在我的控制器类中创建了一个autowired对象的emailService,并按如下方式模拟我的电子邮件服务
@Controller
public class ContactController {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(ContactController.class);
public static final String FEEDBACK_MODEL_KEY="feedback";
private static final String CONTACT_US_VIEW_NAME="contact/contact";
@Autowired
private EmailService emailService;
@RequestMapping(value = "/contact",method = RequestMethod.GET)
public String getContact(ModelMap model){
FeedbackPojo feedbackPojo = new FeedbackPojo();
model.addAttribute(ContactController.FEEDBACK_MODEL_KEY,feedbackPojo);
return ContactController.CONTACT_US_VIEW_NAME;
}
@RequestMapping(value = "/contact",method = RequestMethod.POST)
public String postContact(@ModelAttribute(FEEDBACK_MODEL_KEY) FeedbackPojo feedback){
log.debug("Feedback content {}",feedback);
emailService.sendFeedbackEmail(feedback);
return ContactController.CONTACT_US_VIEW_NAME;
}
}
但是,当我尝试运行我的应用程序时,应用程序终止,我收到的错误如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.subscriptionservice.backend.service.EmailService com.subscriptionservice.web.controllers.ContactController.emailService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.subscriptionservice.backend.service.EmailService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
有人可以在我的实施中指出问题。谢谢。
答案 0 :(得分:3)
没有类型的限定bean 找到[com.subscriptionservice.backend.service.EmailService] 依赖
据我所知,您尚未将接口EmailService
的实例引入Spring管理的bean集合中。
尝试使用MockEmailService
对@Service
进行注释(刻板印象)。
也...
component-scan
,其中包含MockEmailService
所在的包。MockEmailService
在您的申请中永久实施EmailService
合同? (根据名称,我不会这么认为。)如果MockEmailService
仅用于某些情况 - 例如非生产部署或测试 - 您可能需要一个更灵活的系统来定义或连接您的豆子。有很多选项,例如Spring Profiles。