让Spring Boot 1.4+Thymeleaf 3.0启用SpEL编译器以提高性能!
在之前的帖子中,我介绍了在Spring Boot 1.4中使用Thymeleaf 3.0系列的配置方法。但是,从Thymeleaf-Spring 3.0开始,添加了启用SpEL编译器的选项,通过启用SpEL编译器可以提高评估模板文件中指定的表达式的速度。在GitHub的Issue中,有评论指出可以获得约35%的性能改善。(我认为改善程度取决于表达式的数量和使用方式…)
验证版本
-
- Spring Boot 1.4.1.RELEASE
-
- Thymeleaf 3.0.2.RELEASE
- Thymeleaf-Spring 3.0.2.RELEASE
启用选项
如果想要启用选项,请在Java Config类内准备一个方法来注入SpringTemplateEngine,并将注入的SpringTemplateEngine的setEnableSpringELCompiler方法传入true。
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
// ...
@Autowired
void configureThymeleafSpringTemplateEngine(SpringTemplateEngine templateEngine) {
templateEngine.setEnableSpringELCompiler(true); // trueを指定!!
}
}
总结
虽然我没有评估过性能是否有所改进,也无法从感觉上判断,但是我确认通过使用调试器,SpEL的编译器已经启用了。在当前的Spring Boot版本中,无法使用属性进行自定义,但是当Spring Boot处理Thymeleaf的默认版本变为3.0系列时,我想尝试使用属性进行自定义(我还觉得Spring Boot的开发人员可能会提供支持)。
参考网站
- http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html#performance-improvements