2016-08-01 111 views
0

我爲我的spring-boot應用程序編寫簡單的mailsender。不過,我在創建bean時遇到了問題。這裏是我的MailSender類Spring引導和JavaMailSender NoSuchBeanDefinitionException

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

    public void send() { 
     MimeMessage mail = javaMailSender.createMimeMessage(); 
     try { 
      MimeMessageHelper helper = new MimeMessageHelper(mail, true); 
      helper.setTo("[email protected]"); 
      helper.setReplyTo(""); 
      helper.setFrom("[email protected]"); 
      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 !"); 

    } 

} 

下一個我試圖創建主類自動裝配Autowired 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] 

燦你幫我那個

編輯: 當然我有春天-boot-starter-mail包含在我的build.gradle文件中

+0

您是否正確設置了@ComponentScan? – Jens

+0

不是@SpringBootApplication爲我做的嗎? – user3713267

+0

我已將它添加到我的主類,但我仍然收到相同的錯誤 – user3713267

回答

0

好吧我甚至不知道這怎麼可能,但儘快將類名從「MailSender」更改爲其他任何東西 - 一切都開始正常工作

相關問題