java - NoSuchBeanDefinition异常 : No qualifying bean of type 'org.springframework.mail.MailSender'

标签 java spring configuration javabeans spring-context

我试图在我的 ApplicationListener 中使用 MailSender,通过 Autowiring SpringContext:

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 注释的类或我们可以注入(inject)的 bean。并检查是否添加了@component、@service或其他注释。 也许还会有其他错误。这个来自那里。

关于java - NoSuchBeanDefinition异常 : No qualifying bean of type 'org.springframework.mail.MailSender' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60410162/

相关文章:

java - 仅将 tomcat 7 配置为 servlet 容器

java - 防止 JBoss 7 (Jeeves DBMS) 上 JNDI 数据源的 Oracle 连接自动提交

java - 如何解决最新结果的数据库排序问题

spring - Http 入站网关 : Return HttpStatus

java - 在没有 web.xml 的情况下使用 Spring (Boot) 配置 CAS

java - Spring 集成: Poll HTTP

java - 如何在 java.io.file.listFiles 上获取名称?

JAVA_HOME "bug"

configuration - 0.7.8之前的Nginx如何访问HTTP GET参数?

windows - 如何更改 stack.yaml 在 Windows 上的默认位置?