我正在实现如何使用spring引导发送电子邮件,我试图在visual studio代码中实现这一点。但是它给了我以下错误
我在pom.xml中为电子邮件配置添加了以下两个依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
我的主要引导类:
@SpringBootApplication
@ComponentScan(basePackages = {
"email"
})
public class Application {
public static void main(String[] args) {
Mail mail = new Mail();
mail.setMailFrom("abc@gmail.com");
mail.setMailTo("xyz@gmail.com");
mail.setMailSubject("Hi");
mail.setMailContent("Hope you are doing well");
ApplicationContext ctx = SpringApplication.run(Application.class,
args);
MailService mailService = (MailService) ctx.getBean("mailService");
mailService.sendEmail(mail);
}
我认为我的错误与我上面使用的@ComponentScan(basePackages = {"email"})
注释有关。
有人能帮我解决这个错误吗?
发布于 2020-07-21 12:40:46
由于我们不知道包的结构,所以很难知道在basePackages内部的@ComponentScan
中应该有什么
首先,请将应用程序类移到包结构的一个级别上,以便默认情况下读取其下的所有包,并在组件扫描中删除basePackages。所以,应该是@ComponentScan
也就是说,如果您的所有类都在包com.test.mailer
中,那么应用程序类文件应该在com.test
中
试试这个,让我们知道,我也希望您有@Service
注释为@Service("mailService")
更新:,因为用户稍后更新了这个问题,我将发布对他有用的解决方案。
他把这个班提升到一级,去掉了basePackages,它对他起了作用。正如我在答覆的第一部分所述。
或者,他可以在相同的结构中将@ComponentScan(basePackages = {"email"})
更改为@ComponentScan("java.risknucleus")
。
https://stackoverflow.com/questions/63014126
复制相似问题