From ac73766f9b5c7196cf2610f9ff8f36eddac38279 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 13 Feb 2025 14:55:07 +0200 Subject: [PATCH 1/2] Do not auto-configure ViewResolver if spring-webmvc is absent See gh-44259 Signed-off-by: Dmytro Nosan --- .../thymeleaf/ThymeleafAutoConfiguration.java | 4 +++- .../ThymeleafServletAutoConfigurationTests.java | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index e497a68b573..6db961ccc1c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,6 +56,7 @@ import org.springframework.security.web.server.csrf.CsrfToken; import org.springframework.util.MimeType; import org.springframework.util.unit.DataSize; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; +import org.springframework.web.servlet.view.AbstractCachingViewResolver; /** * {@link EnableAutoConfiguration Auto-configuration} for Thymeleaf. @@ -142,6 +143,7 @@ public class ThymeleafAutoConfiguration { } @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(AbstractCachingViewResolver.class) static class ThymeleafViewResolverConfiguration { @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index 4ddea183ca2..205069e8eca 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.testsupport.BuildOutput; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; import org.springframework.context.annotation.Bean; @@ -336,6 +337,13 @@ class ThymeleafServletAutoConfigurationTests { }); } + @Test + @ClassPathExclusions("spring-webmvc-*.jar") + void missingAbstractCachingViewResolver() { + this.contextRunner + .run((context) -> assertThat(context).hasNotFailed().doesNotHaveBean("thymeleafViewResolver")); + } + @Configuration(proxyBeanMethods = false) static class LayoutDialectConfiguration { From 6aa0055e5edeaa3ed233d90a81d8ce22d5714714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 17 Feb 2025 11:01:23 +0100 Subject: [PATCH 2/2] Polish "Do not auto-configure ViewResolver if spring-webmvc is absent" See gh-44259 --- .../thymeleaf/ThymeleafServletAutoConfigurationTests.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index 205069e8eca..9f58eefb4a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -46,7 +46,6 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.testsupport.BuildOutput; -import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; import org.springframework.context.annotation.Bean; @@ -61,6 +60,7 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.support.RequestContext; +import org.springframework.web.servlet.view.AbstractCachingViewResolver; import static org.assertj.core.api.Assertions.assertThat; @@ -338,9 +338,8 @@ class ThymeleafServletAutoConfigurationTests { } @Test - @ClassPathExclusions("spring-webmvc-*.jar") void missingAbstractCachingViewResolver() { - this.contextRunner + this.contextRunner.withClassLoader(new FilteredClassLoader(AbstractCachingViewResolver.class)) .run((context) -> assertThat(context).hasNotFailed().doesNotHaveBean("thymeleafViewResolver")); }