0

源代码是

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。无法弄清楚这些问题有什么问题:(

4

2 回答 2

4

使其MailSenzer成为 Spring bean,并将JavaMailSender自动连接。Spring不会将 bean 自动连接到使用new(@Affe 提到的)创建的对象中。如果您开始使用它的 IoC 容器,请尝试构建更多“类似 Spring”的系统 :)

@Service
public class MailSender {

@Autowired
private JavaMailSender mailSender;

// other stuff 

}

public class SomeBusinessLogicClass {

@Autowired
private MailSender mailSender;

// other stuff

}
于 2013-02-21T06:57:37.653 回答
1

当您使用 new 创建 bean 时,您必须处理它,并且不能自动装配。

如果想使用 Autowired,则使用任何注释使其成为 spring bean。

于 2013-02-21T08:56:08.350 回答