无法在 thymeleaf 中自动装配 TemplateEngine?

unable to autowire TemplateEngine in thymeleaf?

这是我使用 java spring 和 thymeleaf 模板引擎发送 html 电子邮件的代码。

@Service
public class EmailServiceImpl implements EmailService {
    private static final String EMAIL_SIMPLE_TEMPLATE_NAME = "html/html";
@Value("${email.user.register.body}")
private String USER_REGISTER_MESSAGE_BODY;

@Value("${email.user.register.subject}")
private String USER_REGISTER_MESSAGE_SUBJECT;

@Value("${mailSender.address}")
private String SENDER_EMAIL_ADDRESS;

@Autowired
private JavaMailSender mailSender;

@Autowired
private TemplateEngine templateEngine;

@Override
public void sendEmail(MimeMessagePreparator preparator) {
    mailSender.send(preparator);
}

@Async
@Override
public void sendUserRegisterEmail(String receiver, String receiverEmailAddress){
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setSubject(USER_REGISTER_MESSAGE_SUBJECT);
            message.setTo(receiverEmailAddress);
            message.setFrom(SENDER_EMAIL_ADDRESS);
            message.setText(String.format(USER_REGISTER_MESSAGE_BODY, receiver));
        }
    };
    sendEmail(preparator);
}

public void sendMailWithInline(
        final String recipientName, final String recipientEmail, final String imageResourceName,
        final byte[] imageBytes, final String imageContentType, final Locale locale)
        throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("subscriptionDate", new Date());
    ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message =
            new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
    message.setSubject("Example HTML email with inline image");
    message.setFrom("adbuylk@gmail.com");
    message.setTo(recipientEmail);

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process(EMAIL_SIMPLE_TEMPLATE_NAME, ctx);
    message.setText(htmlContent, true); // true = isHtml

    // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
    message.addInline(imageResourceName, imageSource, imageContentType);

    // Send mail
    this.mailSender.send(mimeMessage);

}

我在尝试使用 Jetty 和 Intellij idea 运行 时遇到以下错误。

    [WARNING] Failed startup of context o.e.j.m.p.JettyWebAppContext@23321be7{/adsops,file:///E:/Projects/ADpost/ops/dev/src/main/webapp/,STARTING}{file:///E:/Projects/ADpost/ops/dev/src/main/webapp/}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationUserServiceImpl': Unsatisfied dependency expressed through field 'emailService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailServiceImpl': Unsatisfied dependency expressed through field 'templateEngine'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine' defined in com.vlclabs.adsops.configuration.WebApplicationConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring4.SpringTemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect

这些是依赖项

  <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>${thymeleaf.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-conditionalcomments</artifactId>
        <version>${thymeleaf-extras-conditionalcomments.version}</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
        <version>${thymeleaf-extras-java8time.version}</version>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>${thymeleaf-extras-springsecurity4.version}</version>
    </dependency>

在尝试查看错误消息时,我了解到错误可能是由于我在添加模板引擎时的错误造成的。但是我无法修复它尝试了好几天。请帮忙

问题可能是您混用了不兼容的版本。您可以通过 运行:

检查您的版本
mvn dependency:tree

(如果你有 Eclipse,你可以打开 pom 文件并切换到 "Dependency Hierarchy" 选项卡。)

特别检查这些:

  • org.thymeleaf:thymeleaf-spring4:jar
  • org.thymeleaf:百里香叶:罐装

这两个应该有相同的版本。如果他们不这样做,请调整您的依赖项。