Tobias Erdle's Blog

Development Engineer working with different technologies. Former Jakarta MVC & Eclipse Krazo Committer. All views are my own.

Github: erdlet | E-Mail: blog (at) erdlet (punkt) de

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;

import nz.net.ultraq.thymeleaf.LayoutDialect;

public class CustomThymeleafEngineProducer extends DefaultTemplateEngineProducer {

    @Override
    @Produces
    @Specializes
    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.