源代码是
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中的Custom Tag Library发生的。我试图访问一个工作的dao,但这也给自动连线的sqlSession带来了麻烦。无法找出这些问题出了什么问题:(
最佳答案
当您使用new创建一个bean时,您必须处理它,并且不能对其进行自动装配。
如果要使用“自动装配”,请使用任何注释使它成为弹跳豆。