gpt4 book ai didi

java - 无法在 thymeleaf 中 Autowiring TemplateEngine?

转载 作者:行者123 更新时间:2023-12-02 02:37:34 26 4
gpt4 key购买 nike

这是我的代码,我用它来使用 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:thymeleaf:jar

这两个应该具有相同的版本。如果没有,请调整您的依赖项。

关于java - 无法在 thymeleaf 中 Autowiring TemplateEngine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com