源代码是
public class MailSenzer {
@Autowired
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; }
public static void send(MailForm mail) {
new MailSenzer().senz(mail);
}
private void senz(MailForm mail) {
MimeMessage msger = mailSender.createMimeMessage();
try {
MimeMessageHelper msg = new MimeMessageHelper(msger, mail.getEncoding());
msg.setSubject(mail.getSubject());
msg.setText(mail.getContent(), true);
msg.setFrom(new InternetAddress(mail.getFrom()));
msg.setTo(new InternetAddress(mail.getTo(), mail.getTo_name(), mail.getEncoding()));
if(mail.getAttachment() != null) {
msg.addAttachment(MimeUtility.encodeText(mail.getAttachment_name(), "euc-kr", "B"), mail.getAttachment());
}
} catch(Exception ex) { System.out.println(ex); }
try {
mailSender.send(msger);
} catch(Exception ex) { System.out.println(ex); }
}
}
这是我写的代码。我在控制器中完全测试了相同的代码,但是,当我尝试以静态方法运行时,它给了我错误。
java.lang.NullPointerException
at kr.co.ubplay.utility.mail.MailSenzer.senz(MailSenzer.java:25)
at kr.co.ubplay.utility.mail.MailSenzer.send(MailSenzer.java:21)
at kr.co.ubplay.service.b2biz.controller.CoopEmailController.coop_send(CoopEmailController.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
拥有npe的行是
MimeMessage msger = mailSender.createMimeMessage();
我有一个名为com.ubis的通用软件包用于扫描,所有软件包都在该域下。
jsp 2.0中的自定义标记库正在发生这种情况。我试图访问一个工作dao,但这也给了一个自动装配sqlSession的npe。无法弄清楚这些问题有什么问题:(
答案 0 :(得分:4)
让MailSenzer
成为一个Spring bean,JavaMailSender
将自动连线。 Spring
不会自动将bean连接到使用new
创建的对象(由@Affe提及)。如果你开始使用它的IoC容器,尝试构建更多'Spring like'系统:)
@Service
public class MailSender {
@Autowired
private JavaMailSender mailSender;
// other stuff
}
public class SomeBusinessLogicClass {
@Autowired
private MailSender mailSender;
// other stuff
}
答案 1 :(得分:1)
当你使用new创建一个bean时,你必须处理它,同样不能自动装配。
如果想使用Autowired,请使用任何注释将其设为spring bean。