栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot2 nacos JavaMailSenderImpl not found

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot2 nacos JavaMailSenderImpl not found

背景:springboot2 nacos2.0.x 版本

项目添加了mail依赖:


    org.springframework.boot
    spring-boot-starter-mail

nacos中也有mail相关配置:

spring.mail.username=xx
spring.mail.password=xx
spring.mail.host=smtp.qq.com

但是项目还是报错:

Description:

A component required a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSenderImpl' in your configuration

那么问题出现在哪里呢?这要从源码说起……

从springboot的尿性,呃,不,优秀特性,我们知道,自动装配要从AutoXXX类开始,而mail的自动配置在MailSenderAutoConfiguration,所以我们打开它:

@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)  // 【1.看这里!!!】
@EnableConfigurationProperties(MailProperties.class)  //【2.这里】
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })  // 【3.这里】
public class MailSenderAutoConfiguration {
	
	static class MailSenderCondition extends AnyNestedCondition {
		MailSenderCondition() {
			super(ConfigurationPhase.PARSE_CONFIGURATION);
		}
		@ConditionalOnProperty(prefix = "spring.mail", name = "host")  // 【1.1】
		static class HostProperty {
		}
		@ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name")
		static class JndiNameProperty {
		}
	}
}

上面的点2处 和 我们的配置spring.mail.xxx绑定,这也是我上面 nacos mail配置的由来;
上面的点3处就是给我们创建JavaMailSenderImpl对象了,它使用到了点2处的MailProperties配置;

重点是上面的点1处,引申到上面的 【1.1】处,一句话:
你需要在application.yml/properties文件中额外配置spring.mail.host=xxxx

目前不清楚是不是因为springboot先加载上面的mail自动配置类,然后再加载nacos的原因,虽然我们在nacos中配置了spring.mail.host=xxx,但是由于优先级没能加载到,导致文章开头的报错,望大佬看到后解惑,

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956252.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号