我正在尝试实施一个弹簧电子邮件服务,为此我实施了GOOF设计模式。我创建了如下界面电子邮件服务。
公共接口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());
}
}
然后,在控制器类中创建emailService的Autowired对象,并如下模拟我的emailservice
@Controller
公共类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)}
有人可以指出我实施中的问题。谢谢。
最佳答案
没有合格的Bean类型
找到[com.subscriptionservice.backend.service.EmailService]
依赖
据我所知,您尚未将接口EmailService
的实例引入Spring管理的bean的集合中。
尝试用MockEmailService
注释(定型)@Service
。
也...
确保以某种方式定义了component-scan
,其中将包括MockEmailService
所在的软件包。
您是否打算MockEmailService
在您的应用程序中永久实施EmailService
的合同? (基于名称,我不会这么认为。)如果MockEmailService
仅用于某些情况(例如非生产部署或测试),则可能需要一个更灵活的系统来定义或连接bean。有很多选项,例如Spring Profiles。