繁体   English   中英

Spring引导和JavaMailSender NoSuchBeanDefinitionException

[英]Spring boot and JavaMailSender NoSuchBeanDefinitionException

我正在为我的spring-boot应用程序编写简单的mailsender。 但是我在创建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”更改为其他任何东西 - 一切都开始完美运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM