Field javaMailSender in com.example.emos.wx.task.EmailTask required a bean of type ‘org.springframew

注意:赶时间的伙伴直接看最下面,中间讲的是我的思路过程,想看就可以看看,编程可不是一蹴而就的

 报错如图,我们还是首先将他翻译过来才能知道他在说什么

APPLICATION FAILED TO START  应用程序无法启动

Field javaMailSender in com.example.emos.wx.task.EmailTask required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
com.example.emos.wx.task.EmailTask中的字段javaMailSender需要一个“org.springframework.mail.javamail.javaMailSender”类型的bean,但找不到该bean。


The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
注入点具有以下注释:
-@org.springframework.beans.factory.annotation.Autowired(必需=true)


Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
考虑在配置中定义一个类型为“org.springframework.mail.javamail.JavaMailSender”的bean。

 就是说我注入的javaMailSender没有生效,spring boot认为他不是一个bean

所以我去找了javaMailSender的有关代码,如图

既然时bean的问题,首先要去检查是否在该类上加了@Component的注解,加了之后如果还是如上图所示,请接着往下看

思路是,既然@Autowired注入不了,那就换一个@Qualifier,我也确实是这样做了,而且报错消失了,JVM正常运行,如图

 

 可问题真的解决了吗?实际上并没有,在我进行调试的时候发现新的报错,如图

按照惯例,我们还是先来看一下这个报错,我已经翻译过来了

23:28:30  ERROR  Unexpected exception occurred invoking async method: public void com.example.emos.wx.task.EmailTask.sendAsync(org.springframework.mail.SimpleMailMessage)
java.lang.NullPointerException: null
    at com.example.emos.wx.task.EmailTask.sendAsync(EmailTask.java:26)
    at com.example.emos.wx.task.EmailTask$$FastClassBySpringCGLIB$$c89c0ad4.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:750)
    
    
    
23:28:30错误调用异步方法时发生意外异常:public void com.example.emos.wx.task.EmailTask.sendAsync(org.springframework.mail.SimpleMailMessage)

java.lang.NullPointerException:null

位于com.example.emos.wx.task.EmailTask.sendAsync(EmailTask.java:26)

位于com.example.emos.wx.task.EmailTask$$FastClassBySpringCGLIB$$c89c0ad4.invoke(<generated>)

位于org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

位于org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAupProxy.java:771)

在org.springframework.aop.framework.ReflectiveMethodInvocation.processe(ReflectiveMethodInvocation.java:163)

在org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAupProxy.java:749)

位于org.springframework.aop.interceptor.AncExecutionInterceptor.alambda$invoke$0(AsyncExecutionInterceptor.java:115)

在java.util.concurrent.FutureTask.run(FutureTask.java:266)

位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutoer.java:1149)

在java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

在java.lang.Thread.run(Thread.java:750)

 

 说是java.lang.NullPointerException: null的异常

既然是空指针,按照惯例,就应该打断点了,具体的图片在我测试的时候忘记截图了,断电的结果是在

private JavaMailSender javaMailSender;

 处是空的,也就是javaMailSender是null。

我尝试了很多方法,包括将@Component注解换成@ComponentScan,还是没用,网上的方法也找了好久

最后找到了解决方案:

yml配置文件中email所属的节点不是spring,请检查你的email配置的节点是否在spring的下面,而不是spring的其他节点下,这是yml文件中很容易遇到的问题,使用properties就不会有这样的坑,但是yml确实很简洁,有利有弊吧。

希望对你们有所帮助

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! To define a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration, you can follow these steps: 1. Make sure you have the necessary dependencies in your project for Spring's Email support and JavaMailSender. You can add the following dependency to your Maven pom.xml file: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. Create a configuration class or update an existing one to define the JavaMailSender bean. An example configuration class could look like this: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; @Configuration public class MailConfig { @Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); // Set mailSender properties like host, port, username, password, etc. mailSender.setHost("your-smtp-host"); mailSender.setPort(587); mailSender.setUsername("your-username"); mailSender.setPassword("your-password"); // You can also set other properties like TLS, SSL, etc. return mailSender; } } ``` 3. Customize the properties of the JavaMailSender according to your SMTP server configuration. For example, set the host, port, username, password, etc. based on your SMTP provider's settings. 4. Use the JavaMailSender bean in your code by injecting it into your classes where you need to send emails. For example: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { private final JavaMailSender mailSender; @Autowired public EmailService(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` That's it! Now you have defined a JavaMailSender bean in your configuration and can use it to send emails in your application. Remember to customize the properties according to your SMTP server settings.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值