我试图通过自动装配SpringContext在ApplicationListener中使用MailSender:

package example.util.emailUtil;

@Component
public class PostUserListener implements ApplicationListener<OnPostUserDataEvent> {

@Autowired
private MailSender mailSender;
}


配置类为:

 @ComponentScan(basePackages ={"example.util.emailUtil","example.model","example.service"})
 @Configuration
 public class MailProvider {

   @Bean(name = "mailSender")
   public MailSender javaMailService()
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost("smtp.gmail.com");
    javaMailSender.setPort(587);
    javaMailSender.setProtocol("smtp");
    javaMailSender.setUsername("sender’s email");
    javaMailSender.setPassword("sender’s password");
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", "true");
    mailProperties.put("mail.smtp.starttls.enable", "true");
    mailProperties.put("mail.smtp.debug", "true");
    javaMailSender.setJavaMailProperties(mailProperties);
    return javaMailSender;
   }
 }


但是我发现了以下例子:

 NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.MailSender' available: expected at least 1 bean which qualifies as autowire candidat

最佳答案

它不将您的MailSender类视为可以用@autowired注释的类,也不视为我们可以注入的bean。并检查是否添加了@ component,@ service或其他注释。
也许还会有其他错误。这个来自那里。

关于java - NoSuchBeanDefinitionException:没有类型为'org.springframework.mail.MailSender'的合格bean,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60410162/

10-09 00:16