@Async不与Thymeleaf模板引擎一起使用

时间:2015-09-16 07:56:03

标签: spring spring-mvc spring-security thymeleaf

    @Async
    public void sendEmail(String to, String subject, WebContext context,String template) throws MessagingException
    {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
        helper.setTo(to);
        helper.setFrom("some.email@mail-server.com");
        helper.setSubject(subject);
        System.out.println("content1");  //able to see this in console
        String content=templateEngine.process(template, context); //problem with this
        System.out.println("content2");  // not able to see this in console
        helper.setText(content, true);
        mailSender.send(mimeMessage);
    }

我无法发送带有@Async注释的电子邮件。当我删除此注释时,电子邮件工作。 @Async无法使用Thymeleaf模板引擎。我将@EnableAsync放在RootConfig上,我正在创建bean

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

听起来您的Async未启用请查看本教程,了解打开它的最佳方法。

http://blog.adaofeliz.com/2014/11/02/spring-mvc-without-xml-configuration/

总之,在启动时,您需要确保为DispatcherServlet注册以下选项:

servlet.setAsyncSupported(true);

根据评论讨论,请找到一个类似的提出的问题围绕Async不工作和用户必须强制注册它:

Spring @Async Not Working