Tobias Erdle's Blog

Writes mostly about Java, Jakarta EE and software development in general. All views are my own.

Github: erdlet | E-Mail:

Krazo Thymeleaf extension: How to override default TemplateEngine producer

While I was working on a small MVC application during the last weeks, I struggled a little bit when trying to override the default produced TemplateEngine of Krazo's Thymeleaf extension. This post will explain, how to override the default to be able to configure Thymeleaf add-ons like the layout dialect.

In my application, I use this class to override the DefaultTemplateEngineProducer of Krazo's Thymeleaf extension:

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Specializes;

import org.eclipse.krazo.ext.thymeleaf.DefaultTemplateEngineProducer;
import org.thymeleaf.TemplateEngine;


public class CustomThymeleafEngineProducer extends DefaultTemplateEngineProducer {

    public TemplateEngine getTemplateEngine() {

        final TemplateEngine templateEngine = super.getTemplateEngine();
        templateEngine.addDialect(new LayoutDialect());

        return templateEngine;

There is nothing really special about it on the first look, as it simply overrides DefaultTemplateEngineProducer and getTemplateEngine. But on a second look, you might see the javax.enterprise.inject.Specializes annotation on the producer method. This annotation leads to a deactivation of the default TemplateEngine in the CDI container and a inject of our custom TemplateEngine in all consumers.