diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 0d5849a9753..2189332b98e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -151,6 +151,7 @@ public class ThymeleafAutoConfiguration { protected static class ThymeleafWebLayoutConfiguration { @Bean + @ConditionalOnMissingBean public LayoutDialect layoutDialect() { return new LayoutDialect(); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index 81075f9c4e7..0b426406f52 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -20,6 +20,8 @@ import java.io.File; import java.util.Collections; import java.util.Locale; +import nz.net.ultraq.thymeleaf.LayoutDialect; +import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -31,12 +33,16 @@ import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.TemplateResolver; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.OutputCapture; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; @@ -230,4 +236,24 @@ public class ThymeleafAutoConfigurationTests { assertThat(this.context.getBean(ResourceUrlEncodingFilter.class)).isNotNull(); } + @Test + public void layoutDialectCanBeCustomized() throws Exception { + this.context.register(LayoutDialectConfiguration.class); + this.context.refresh(); + LayoutDialect layoutDialect = this.context.getBean(LayoutDialect.class); + assertThat(ReflectionTestUtils.getField(layoutDialect, "sortingStrategy")) + .isInstanceOf(GroupingStrategy.class); + } + + @Configuration + @ImportAutoConfiguration({ ThymeleafAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class }) + static class LayoutDialectConfiguration { + + @Bean + public LayoutDialect layoutDialect() { + return new LayoutDialect(new GroupingStrategy()); + } + } + }