gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:23 28 4
gpt4 key购买 nike

我试图在我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com