我尝试在Spring中使用邮件服务。我使用的是SpringBoot2.1.7.Release版本和amazon-sqs-java消息库1.0.8版本。
我的代码看起来像:
@Service
@RequiredArgsConstructor
public class MailService {
private static final Logger logger = Logger.getLogger(MailService.class.getName());
private final AmazonSQS amazonSQS;
@Value("${aws.sqs.queue.mail}")
private final String sqsQueueMail;
public void sendMail(final SQSMailParams mailParams) {
final String queueUrl = amazonSQS.getQueueUrl(sqsQueueMail).getQueueUrl();
try {
final String messageBody = SQSMailParams.createSQSMailParams(mailParams, mailParams.getTemplateKey(), mailParams.getProcessId()).toJson();
final SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, messageBody);
sendMessageRequest.setMessageGroupId("TOLL-BOX-MAIL");
amazonSQS.sendMessage(sendMessageRequest);
logger.info("TOLL-BOX mail added to mail queue");
} catch (JsonProcessingException e) {
logger.error("Mail cannot be added to the mail queue: " + String.join(",", mailParams.recipients) + "." + e);
}
}
}
但我通过运行代码得到了以下失败。
2020-03-02 07:34:00.242 ERROR 10568 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in de.svg.tollbox.service.MailService 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.
Disconnected from the target VM, address: '127.0.0.1:43565', transport: 'socket'
Process finished with exit code 1
有人能告诉我我做错了什么吗?谢谢。
从sqsQueueMail
中删除final
。
@Value("${aws.sqs.queue.mail}")
private String sqsQueueMail;
这应该能解决你的问题。不知怎的,对于@RequiredArgsConstructor
,变量的@Value
注释没有得到解析,这使得应用程序需要查找String
类型的bean
在项目根目录中创建lombok.config
文件,并添加以下行:
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value
这将允许您将@Value
与final
字段和@RequiredArgsConstructor
一起使用