为什么MailComponent无法找到类型为“ org.thymeleaf.TemplateEngine”的bean?

时间:2020-03-08 23:41:17

标签: spring spring-boot thymeleaf

我做项目,我需要在Spring Boot中做联系表。 我的MailComponent找不到类型为“ org.thymeleaf.TemplateEngine”的bean? 我在ThymelefConfig中。 我该如何在配置中定义类型为“ org.thymeleaf.TemplateEngine”的bean?

所有错误文本:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-08 23:57:21.518 ERROR 3512 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field templateEngine in MailComponent required a bean of type 'org.thymeleaf.TemplateEngine' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'templateEngine' in 'ThymeleafAutoConfiguration.ThymeleafDefaultConfiguration' not loaded because @ConditionalOnBean (types: org.thymeleaf.spring5.ISpringTemplateEngine; SearchStrategy: all) found beans of type 'org.thymeleaf.spring5.ISpringTemplateEngine' templateEngine
    - Bean method 'templateEngine' in 'ThymeleafAutoConfiguration.ThymeleafReactiveConfiguration' not loaded because did not find reactive web application classes


Action:

Consider revisiting the entries above or defining a bean of type 'org.thymeleaf.TemplateEngine' in your configuration.

MailComponent:

@Component
@ComponentScan(basePackages ="com.diet4you.LapkoEkaterina.ThymeleafConfig")
@ComponentScan(basePackages ="com.diet4you.LapkoEkaterina.MailConfig")
public class MailComponent {

    @Autowired
    MailSender mailSender;

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    TemplateEngine templateEngine;



    public boolean sendSimpleMail(Contact contact) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(contact.getEmail());
        mailMessage.setSubject(contact.getSubject());
        mailMessage.setText(contact.getMessage());
        mailMessage.setTo("email@email.com"); // if you use Gmail do not forget to put your personal address

        try {
            mailSender.send(mailMessage);
            return true;
        } catch (MailException e) {
            System.err.println(e.getMessage());
            return false;
        }
    }

    public boolean sendHtmlMail(Contact contact) {

        Context context = new Context();
        context.setVariable("contact", contact);
        final String messageHtml = templateEngine.process("email/contact", context);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mailMessage = new MimeMessageHelper(mimeMessage);
        try {
            mailMessage.setTo("email@email.com"); // if you use Gmail do not forget to put your personal address
            mailMessage.setFrom(contact.getEmail());
            mailMessage.setSubject(contact.getSubject());
            mailMessage.setText(messageHtml, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (MessagingException | MailException e) {
            System.err.println(e.getMessage());
            return false;
        }
    }
}

MailConfig:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:ValidationMessages.properties")
@Configuration
public class MailConfig {

    @Value("${spring.mail.host}")
    private String host;

    @Value("${spring.mail.port}")
    private int port;

    @Value("${spring.mail.default-encoding}")
    private String encoding;

    @Value("${spring.mail.properties.mail.debug}")
    private String debug;




    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setDefaultEncoding(encoding);
        mailSender.setHost(host);
        mailSender.setPort(port);



        Properties properties = new Properties();

        properties.put("mail.debug", debug);
        mailSender.setJavaMailProperties(properties);

        return mailSender;
    }

}

ThymeleafConfig:

@Configuration
public class ThymeleafConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setTemplateEngine( templateEngine());
        return viewResolver;
    }

    @Bean
    public ISpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/static/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        return templateResolver;
    }
}

0 个答案:

没有答案