java - 类需要一个类型为 'java.lang.String' 的 bean,但无法找到

标签 java spring-boot java.lang.class

我尝试在 Spring 中使用邮件服务。我使用 Spring-Boot 版本 2.1.7.Release 和 amazon-sqs-java-messaging-lib 版本 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

关于java - 类需要一个类型为 'java.lang.String' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60483725/

相关文章:

java - JUnit AssertEquals 失败

spring - Spring Boot中基于​​表达式的Autowire(使用Kotlin)

javascript - Spring Boot 不提供静态 javascript 文件

java - Java中杀死进程

java - 带有继承的Java程序应该如何追溯?

java - 循环图

java - Websphere Portal portlet 不会在异常后失败并在重新加载时重新触发所有事件

java - IntelliJ IDEA生成的Spring Boot项目Artifact导致错误

java - 通过读取类的源(.java)文件获取类的 java.lang.Class 对象

java.lang.ClassCastException : android. widget.TextView 无法转换为 android.widget.EditText