我一直在学习如何制作 Java Spring Web 服务,但在尝试发送邮件时遇到了问题。
当我尝试通过我的 Controller 发送它时,代码工作正常,但我试图在一个单独的类中获取代码,我突然得到一个 NullPointerException
,即使代码是相同(据我所知)。
这是当前出现问题的函数:
package com.tenshin_arts.inventory.mail;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import com.tenshin_arts.inventory.dao.InventoryItem;
import com.tenshin_arts.inventory.dao.User;
import com.tenshin_arts.inventory.service.InventoryService;
public class InventoryMail {
@Autowired
private MailSender mailSender;
public void stockBelowMinimumUpdate(InventoryItem inventoryItem) {
String email = "xxxx@gmail.com";
String text= "";
text += "Het volgende product is onder het minimum gekomen:\n\n";
text += "Productnaam: " + inventoryItem.getName();
text += "\nProductcode: " + inventoryItem.getBarcode();
text += "\nHuidige Voorraad: " + inventoryItem.getCurrentStock();
text += "\nMinimum Voorraad: " + inventoryItem.getStockMinimum();
text += "\nPrijs: " + inventoryItem.getPrice();
text += "\nLeverancier: " + inventoryItem.getSupplier();
text += "\n\n\nBericht voor de leverancier:";
text += "\n\n" + inventoryItem.getBarcode();
text += "\t" + inventoryItem.getName();
text += "\t[aantal hier] x €" + inventoryItem.getPrice();
System.out.println("email running");
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("xxxxx@gmail.com");
mail.setTo(email);
mail.setSubject("Test is gelukt!");
mail.setText(text);
try {
mailSender.send(mail); //This line produces the exception
} catch (Exception e) {
e.printStackTrace();
System.out.println("Can't send message");
}
}
}
这是堆栈跟踪:
java.lang.NullPointerException
at com.tenshin_arts.inventory.mail.InventoryMail.stockBelowMinimumUpdate(InventoryMail.java:53)
at com.tenshin_arts.inventory.dao.InventoryDao.checkStockForMail(InventoryDao.java:152)
at com.tenshin_arts.inventory.service.InventoryService.checkStockForMail(InventoryService.java:52)
at com.tenshin_arts.inventory.controllers.InventoryController.databaseCallMutateItem(InventoryController.java:144)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
这是我的 bean 配置:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="xxx@gmail.com" />
<property name="password" value="xxxxx" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
</props>
</property>
</bean>
奇怪的是,在我的映射 Controller 中,我有以下运行良好的代码(删除了与邮件无关的函数):
package com.tenshin_arts.inventory.controllers;
import java.security.Principal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tenshin_arts.inventory.dao.InventoryItem;
import com.tenshin_arts.inventory.dao.User;
import com.tenshin_arts.inventory.service.InventoryService;
import com.tenshin_arts.inventory.service.UsersService;
@Controller
public class InventoryController {
private InventoryService inventoryService;
private InventoryItem currentItem;
private UsersService usersService;
@Autowired
private MailSender mailSender;
@RequestMapping(value="/sendmessage")
@ResponseBody
public void sendMessage(Principal principal) {
UserDetails userDetails =
(UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
List<InventoryItem> inventoryItemsList = inventoryService.getCurrent();
User currentUser = usersService.getUserEmail(userDetails.getUsername());
String email = currentUser.getEmail();
String text= "";
for(InventoryItem s : inventoryItemsList) {
text += s.getBarcode();
text += "\t";
text += s.getName();
text += "\t";
text += s.getCurrentStock();
text += "\t";
text += s.getPrice();
text += "\n";
}
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("xxxxx@gmail.com");
mail.setTo(email);
mail.setSubject("Test is gelukt!");
mail.setText(text);
try {
mailSender.send(mail);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Can't send message");
}
}
}
我已经通过 system.out
检查了我的 SimpleMailMessage
“邮件”,所以我认为问题出在我的 MailSender
bean/布线,但我不知道是什么,因为我以相同的方式布线。
你能帮我解决我的问题吗?
请您参考如下方法:
为什么会抛出 NullPointerException?
这表示 mailSender
为 null
。它是 null
,因为 Spring 无法将 bean 实例 Autowiring 到 mailSender
。 Spring 无法做到这一点,因为它无法判断 InventoryMail
本身是一个 bean。因为 Spring 无法判断 InventoryMail
是一个 bean,所以它不会检测到 @Autowired
注释。
为什么它在 InventoryController
中起作用?
那是因为注解@Controller
告诉Spring,InventoryController
是一个bean,从而使Spring能够检测到@Autowired
。
建议的解决方案
使用 @Component
注释您的 InventoryMail
类以告诉 Spring InventoryMail
是一个 bean。这使 Spring 能够检测到 @Autowired
。