当前位置: 首页 > 知识库问答 >
问题:

MailComponent中的字段templateEngine需要org.thymeleaf.TemplateEngine类型的bean,但找不到

白越
2023-03-14

我是Spring新来的,所以我有一个这样的问题:描述:

字段templateEngine在com.diet4you。拉普科·叶卡捷琳娜。MailComponent需要找不到类型org.thymeleaf.TemplateEngine的bean。

注入点具有以下注释:-@org。springframework。豆。工厂注释。自动连线(必需=真)

已找到以下候选项,但无法注入:-“ThymeleafAutoConfiguration”中的Bean方法“templateEngine”。ThymeleafDefaultConfiguration“未加载,因为@ConditionalOnBean(类型:org.thymeleaf.spring5.ISpringTemplateEngine;SearchStrategy:all)找到了类型为“org”的bean。百里香。Spring五号。ISpringTemplateEngine“templateEngine-Bean方法”templateEngine“在”自动配置中。由于未找到反应性web应用程序类,因此未加载响应性配置

行动:

考虑重新查看上面的条目或定义一个类型为“org”的bean。百里香。配置中的TemplateEngine。

邮件组件:

@Component
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;
        }
    }
}

邮件配置:

@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;

    /**
     * Gmail Config
     */
//    @Value("${spring.mail.username}")
//    private String username;
//    @Value("${spring.mail.password}")
//    private String password;
//    @Value("${spring.mail.properties.mail.smtp.auth}")
//    private String auth;
//    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
//    private String starttls;

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

        /**
         * Gmail Config
         */
//        mailSender.setUsername(username);
//        mailSender.setPassword(password);
        Properties properties = new Properties();
//        properties.put("mail.smtp.auth", auth);
//        properties.put("mail.smtp.starttls.enable", starttls);
        properties.put("mail.debug", debug);
        mailSender.setJavaMailProperties(properties);

        return mailSender;
    }





}

ThymeleafConfig:

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:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        return templateResolver;
    }
}

我从这个例子中做到了:https://www.youtube.com/watch?v=KoPWOACk6CA

共有2个答案

贾实
2023-03-14

正如@Chris所建议的那样。

四处看看application.properties或application.yml文件。

必须有键定义如下所示的例子

application.properties

spring.mail.default-encoding=UTF-8

application.yml

spring
  mail
    default-encodig: UTF-8
樊烨烨
2023-03-14

Spring在试图构建KonsulrecjaController时会抱怨,因为它找不到满足对MailComponent依赖的bean。从您包含的类中,它看起来可能是两件事之一:

  • 您的spring上下文没有TemplateEngine实例。您的MailComponent需要一个,如果您的上下文没有它,它将无法构造MailController(因此将无法构造KonsultacjaController)

  • Spring组件扫描找不到MailController组件,因此上下文中不存在该组件

顺便说一句,JavaMailSender扩展了MailSender,所以您不需要将两者都注入MailComponent。

 类似资料: