# ThymeleafConfig.java

    package cloud.wconnect.finance.config;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.thymeleaf.spring6.SpringTemplateEngine;
    import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;

    /**
     * @author lichen
     * @description thymeleaf 模板引擎配置类
     * @date 2023/4/12 15:44
     */
    @Configuration
    public class ThymeleafConfig {

        /**
         * Spring 容器对象
         * 通过代码配置thymeleaf 模板引擎时，需要配置ApplicationContext。
         * 不然使用引擎对象解析模板时会报错：ApplicationContext is null
         */
        @Autowired
        private ApplicationContext applicationContext;

        /**
         * text文件模块引擎解析对象
         *
         * @return org.thymeleaf.spring6.SpringTemplateEngine
         * @author lichen
         * @date 2023/6/27
         */
        @Bean
        @Qualifier("textEngine")
        public SpringTemplateEngine textEngine(){
            //解析器
            SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
            resolver.setPrefix("classpath:/tmpl-text/");
            resolver.setTemplateMode("TEXT");
            resolver.setSuffix(".txt");
            resolver.setApplicationContext(applicationContext);

            SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
            springTemplateEngine.setTemplateResolver(resolver);
            return springTemplateEngine;
        }

        /**
         * html文件模块引擎解析对象
         *
         * @return org.thymeleaf.spring6.SpringTemplateEngine
         * @author lichen
         * @date 2023/6/27
         */
        @Bean
        @Qualifier("templateEngine")
        public SpringTemplateEngine templateEngine(){
            //解析器
            SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
            resolver.setPrefix("classpath:/tmpl-html/");
            resolver.setTemplateMode("HTML");
            resolver.setSuffix(".html");
            resolver.setApplicationContext(applicationContext);

            SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
            springTemplateEngine.setTemplateResolver(resolver);
            return springTemplateEngine;
        }

    }