Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thymeleaf auto-configuration in a reactive application can fail due to duplicate templateEngine beans #30384

Closed
wilkinsona opened this issue Mar 23, 2022 · 0 comments
Assignees
Labels
type: bug
Milestone

Comments

@wilkinsona
Copy link
Member

@wilkinsona wilkinsona commented Mar 23, 2022

ThymeleafAutoConfiguration has two inner-classes that define beans named templateEngine:

@Configuration(proxyBeanMethods = false)
protected static class ThymeleafDefaultConfiguration {
@Bean
@ConditionalOnMissingBean(ISpringTemplateEngine.class)
SpringTemplateEngine templateEngine(ThymeleafProperties properties,
ObjectProvider<ITemplateResolver> templateResolvers, ObjectProvider<IDialect> dialects) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(properties.isEnableSpringElCompiler());
engine.setRenderHiddenMarkersBeforeCheckboxes(properties.isRenderHiddenMarkersBeforeCheckboxes());
templateResolvers.orderedStream().forEach(engine::addTemplateResolver);
dialects.orderedStream().forEach(engine::addDialect);
return engine;
}
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
static class ThymeleafReactiveConfiguration {
@Bean
@ConditionalOnMissingBean(ISpringWebFluxTemplateEngine.class)
SpringWebFluxTemplateEngine templateEngine(ThymeleafProperties properties,
ObjectProvider<ITemplateResolver> templateResolvers, ObjectProvider<IDialect> dialects) {
SpringWebFluxTemplateEngine engine = new SpringWebFluxTemplateEngine();
engine.setEnableSpringELCompiler(properties.isEnableSpringElCompiler());
engine.setRenderHiddenMarkersBeforeCheckboxes(properties.isRenderHiddenMarkersBeforeCheckboxes());
templateResolvers.orderedStream().forEach(engine::addTemplateResolver);
dialects.orderedStream().forEach(engine::addDialect);
return engine;
}
}

If ThymeleafDefaultConfiguration is processed first, it'll define the templateEngine bean. When ThymeleafReactiveConfiguration is processed, it too will try to define a bean named templateEngine as the existing templateEngine bean is an ISpringTemplateEngine rather than an ISpringWebFluxTemplateEngine.

We need to move the configurations for the template engine out into a separate class and then import them such that ThymeleafReactiveConfiguration is guaranteed to be processed before ThymeleafDefaultConfiguration. This was already fixed on main as part of 26fecbe. We need to back port those changes to 2.5.x and then merge forwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug
Projects
None yet
Development

No branches or pull requests

1 participant