【问题标题】:Spring boot and JavaMailSender NoSuchBeanDefinitionExceptionSpring boot 和 JavaMailSender NoSuchBeanDefinitionException
【发布时间】:2016-12-06 04:35:53
【问题描述】:

我正在为我的 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

【问题讨论】:

  • 你是否正确设置了`@ComponentScan?
  • @SpringBootApplication 不帮我做吗?
  • 我已将它添加到我的 Main 类,但我仍然收到相同的错误
  • 它看起来怎么样?
  • 我上面已经提到了

标签: java spring


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-10-01
    • 1970-01-01
    • 2015-01-14
    • 2019-11-14
    • 2015-03-05
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多