我收到以下错误
Description:
Parameter 0 of constructor in com.gambeat.site.utility.email.EmailStatus required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
这是电子邮件状态类
package com.gambeat.site.utility.email;
import org.springframework.stereotype.Component;
@Component
public class EmailStatus {
public static final String SUCCESS = "SUCCESS";
public static final String ERROR = "ERROR";
private final String to;
private final String subject;
private final String body;
private String status;
private String errorMessage;
public EmailStatus(String to, String subject, String body) {
this.to = to;
this.subject = subject;
this.body = body;
}
public EmailStatus success() {
this.status = SUCCESS;
return this;
}
public EmailStatus error(String errorMessage) {
this.status = ERROR;
this.errorMessage = errorMessage;
return this;
}
public boolean isSuccess() {
return SUCCESS.equals(this.status);
}
public boolean isError() {
return ERROR.equals(this.status);
}
public String getTo() {
return to;
}
public String getSubject() {
return subject;
}
public String getBody() {
return body;
}
public String getStatus() {
return status;
}
public String getErrorMessage() {
return errorMessage;
}
}
this is the EmailSender and HtmlEmailSender which calls the EmailStatus class respectively.
package com.gambeat.site.utility.email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
@Component
public class EmailSender {
private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class);
@Autowired
private JavaMailSender javaMailSender;
public EmailStatus sendPlainText(String to, String subject, String text) {
return sendM(to, subject, text, false);
}
public EmailStatus sendHtml(String to, String subject, String htmlBody) {
return sendM(to, subject, htmlBody, true);
}
private EmailStatus sendM(String to, String subject, String text, Boolean isHtml) {
try {
MimeMessage mail = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text, isHtml);
javaMailSender.send(mail);
LOGGER.info("Send email '{}' to: {}", subject, to);
return new EmailStatus(to, subject, text).success();
} catch (Exception e) {
LOGGER.error(String.format("Problem with sending email to: {}, error message: {}", to, e.getMessage()));
return new EmailStatus(to, subject, text).error(e.getMessage());
}
}
}
HtmlEmailSender类
package com.gambeat.site.utility.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@Component
public class EmailHtmlSender {
@Autowired
private EmailSender emailSender;
@Autowired
private TemplateEngine templateEngine;
public EmailStatus send(String to, String subject, String templateName, Context context) {
String body = templateEngine.process(templateName, context);
return emailSender.sendHtml(to, subject, body);
}
}
当我在服务类
中调用HtmlSendEmail时,问题就出现了 package com.gambeat.site.services.implementation;
import com.gambeat.site.entities.User;
import com.gambeat.site.services.NotificationService;
import com.gambeat.site.utility.email.EmailHtmlSender;
import com.gambeat.site.utility.email.EmailStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
/**
* Created by Oto-obong on 30/10/2017.
*/
@Service
public class DefaultNotificationService implements NotificationService {
private JavaMailSender javaMailSender;
private EmailHtmlSender emailHtmlSender;
@Autowired
public DefaultNotificationService(JavaMailSender javaMailSender, EmailHtmlSender emailHtmlSender){
this.javaMailSender = javaMailSender;
this.emailHtmlSender = emailHtmlSender;
}
@Override
public EmailStatus SignUpConfirmation(User user, String url) {
Context context = new Context();
context.setVariable("salutation", String.format("Hello s%", user.getUserName()));
context.setVariable("title", "Welcome to Gambeat");
context.setVariable("message", "Lorem Lorem Lorem");
context.setVariable("link", url);
EmailStatus emailStatus = emailHtmlSender.send(user.getEmail(), "Welcome to Gambeat", "email/welcome", context);
return emailStatus;
}
@Override
public EmailStatus BroadCastViaEmail(String message) {
return null;
}
@Override
public EmailStatus BroadCastViaEmail(User user, String message) {
return null;
}
@Override
public EmailStatus SendTokenViaEmail(User user, String token) {
Context context = new Context();
context.setVariable("salutation", String.format("Hello s%", user.getUserName()));
context.setVariable("title", "Welcome to Gambeat");
context.setVariable("description", "Lorem Lorem Lorem");
EmailStatus emailStatus = emailHtmlSender.send(user.getEmail(), "Welcome to Gambeat", "email/welcome", context);
return emailStatus;
}
}
根据人们的推荐,我被告知要将电子邮件状态包类添加到我的组件扫描中,但它无济于事,有人可以帮帮我吗?
答案 0 :(得分:1)
EmailStatus
annotation将 @Component
类注册为spring bean。在这种情况下,您需要在类中使用默认构造函数。
@Component
public class EmailStatus {
}
在注册为可以自动装配的弹簧之后
@Autowired
EmailStatus emailStatus;
但在查看您的代码之后,您永远不会自动装配EmailStatus
bean。因此,您可以做的最简单的事情是删除@Component
类上的EmailStatus
注释。这样可以正常工作。
答案 1 :(得分:-1)
问题出在你的EmailStatus
类中,因为你定义了一个覆盖默认值的构造函数,spring不知道如何构建这个类,它不知道如何注入strings
args给承包商public EmailStatus(String to, String subject, String body) {...
如果你需要初始化一些变量,那么删除这个承包商并定义一个默认的承包商(否则,编译器会自动定义它)
使用setters
设置to
,subject
和body