如何在 Spring Boot 中启用 Thymeleaf 的预编译功能?需要引入哪些依赖?
解读
国内 Spring Boot 项目普遍追求“启动秒级、内存省、发布快”。Thymeleaf 默认每次运行时解析模板,虽然开发阶段方便,但生产环境会带来两大痛点:
- 首次访问模板需现场编译,RT 突然升高,压测报告不好看;
- 模板文件留在 jar 之外,不符合等保“代码与配置分离”要求,也不利于容器镜像扫描。
因此面试官问“预编译”,表面是问依赖,实质在考察:
- 是否知道 Thymeleaf 的模板解析与缓存机制;
- 是否能把“预编译”与 Spring Boot 的打包、类加载、Profile 结合起来;
- 是否理解预编译对热部署、灰度的影响。
知识点
- Thymeleaf 的 TemplateResolver 分为 FileTemplateResolver(磁盘)与 ClassLoaderTemplateResolver(classpath)。预编译必须把模板打进 jar,用后者。
- Thymeleaf 3 开始提供 ICacheManager 与 ITemplateResource 抽象,官方给出“template-engine-compile-only”模式,可把解析结果序列化成字节码,启动时直接装载。
- Spring Boot 的 spring-boot-starter-thymeleaf 默认只带 thymeleaf-spring5 核心,不包含预编译工具;需要额外引入 thymeleaf-layout-dialect(非必须)与 thymeleaf-precompiler(官方示例库,国内镜像可用)。
- 预编译 Maven 插件:thymeleaf-maven-plugin(groupId org.thymeleaf),在 compile 阶段把 /templates/*.html 预生成实现了 org.thymeleaf.templateresolver.ITemplateResource 的 .class 文件,输出到 target/classes/META-INF/thymeleaf-precompiled。
- 启用开关:spring.thymeleaf.mode=HTML,spring.thymeleaf.cache=true(默认),再加 spring.thymeleaf.prefix=classpath:/templates/,保证模板从 jar 内加载;最后写配置类把 SpringResourceTemplateResolver 替换为 PrecompiledTemplateResolver。
- 国内公司常把模板当静态资源交给 Nginx,预编译后模板不再暴露,满足“前后端分离+SEO”双重需求,也避免源码泄漏。
答案
步骤一:引入依赖
在 pom.xml 中保留官方 starter,并追加预编译插件与工具库:
<!-- 原依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 预编译核心,Maven 中央仓国内镜像同步无延迟 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-precompiler</artifactId>
<version>3.0.15.RELEASE</version>
<scope>provided</scope> <!-- 仅编译期使用 -->
</dependency>
步骤二:配置 Maven 插件
<plugin>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-maven-plugin</artifactId>
<version>3.0.15.RELEASE</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>precompile</goal>
</goals>
</execution>
</executions>
<configuration>
<templatesDirectory>${project.basedir}/src/main/resources/templates</templatesDirectory>
<outputDirectory>${project.build.outputDirectory}/META-INF/thymeleaf-precompiled</outputDirectory>
</configuration>
</plugin>
步骤三:写 Java 配置,让 Spring Boot 使用预编译结果
@Configuration
@Profile("prod") // 只在生产环境启用,dev 仍用原模板方便调试
public class ThymeleafPrecompiledConfig {
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(precompiledTemplateResolver());
return engine;
}
private ITemplateResolver precompiledTemplateResolver() {
PrecompiledTemplateResolver resolver = new PrecompiledTemplateResolver();
resolver.setOrder(1);
resolver.setCheckExistence(true); // 找不到再回落到文件
return resolver;
}
}
步骤四:打包验证
执行 mvn clean package,观察 target/classes/META-INF/thymeleaf-precompiled 出现 .class 文件;启动 jar 后加调试日志:
logging.level.org.thymeleaf=DEBUG
若日志出现“Template cache hit”且耗时 <1ms,说明预编译生效。
拓展思考
-
热更新权衡:预编译后模板变成 .class,无法通过修改文件实时生效。国内常见做法是
- dev 关闭预编译,用 spring-boot-devtools;
- test 阶段 CI 自动打“预编译包”做性能基线;
- prod 全量预编译,灰度发布时整包替换,保证模板与代码版本一致。
-
与 GraalVM Native Image 结合:预编译生成的字节码可直接进入静态初始区,显著降低 native image 构建时间,国内云厂商函数计算(阿里云 FC、腾讯云 SCF)已验证可行。
-
安全合规:等保 2.0 要求“源代码不可直接访问”,预编译后模板无原始 HTML,可配合 jar 加密(ClassFinal 等)进一步防止反编译,满足金融、政务项目审计要求。