Spring引导和JavaMailSender NoSuchBeanDefinitionException

时间:2016-08-01 07:21:05

标签: java spring

我为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

1 个答案:

答案 0 :(得分:0)

好的,我甚至不知道这是怎么可能的,但是我很快就改变了#34; MailSender"无论如何 - 一切都开始完美运作