gpt4 book ai didi

java - Spring boot 和 JavaMailSender NoSuchBeanDefinitionException

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:32 25 4
gpt4 key购买 nike

我正在为我的 spring-boot 应用程序编写简单的邮件发送器。但是我在创建 Bean 时遇到了问题。这是我的 MailSender 类

@Component
public class MailSender {
@Autowired
private JavaMailSender javaMailSender;

public void send() {
MimeMessage mail = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo("somemail@some.com");
helper.setReplyTo("");
helper.setFrom("me@email.com");
helper.setSubject("Lorem ipsum");
helper.setText("Lorem ipsum dolor sit amet [...]");
} catch (MessagingException e) {
e.printStackTrace();
} finally {}
javaMailSender.send(mail);
System.out.println("Mail has been sent !");

}

}

接下来我尝试在主类中创建自动连接的 MailSender 实例,这就是我收到错误的地方

@SpringBootApplication

public class myApplication {

private static final Logger LOGGER = LoggerFactory.getLogger(myApplication.class);
@Autowired
private MailReceiver mailReceiver;
@Autowired
private MailSender ms;

public static void main(String[] args) {
SpringApplication.run(myApplication.class, "--debug");
}
...
//more code

错误是

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myApplication': Injection
of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private info.some.mail.MailSender
info.some.myApplication.ms; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [info.some.mail.MailSender]

你能帮我吗

编辑:当然,我的 build.gradle 文件中包含 spring-boot-starter-mail

最佳答案

好吧,我什至不知道这怎么可能,但一旦我将类名从“MailSender”更改为其他名称 - 一切就开始完美运行

关于java - Spring boot 和 JavaMailSender NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com