From 5e5864520288818b67cff5313dfa6413ab426404 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 2 Oct 2015 15:35:58 -0700 Subject: [PATCH] Log warning for template folder not found problems Update template auto-configurations to log a warning message rather than throw an exception if the template folder cannot be found. Fixes gh-4075 --- spring-boot-actuator/pom.xml | 15 ++++++++++----- spring-boot-autoconfigure/pom.xml | 11 ++++++----- .../freemarker/FreeMarkerAutoConfiguration.java | 16 +++++++++++----- .../GroovyTemplateAutoConfiguration.java | 17 +++++++++++------ .../mustache/MustacheAutoConfiguration.java | 16 ++++++++++------ .../thymeleaf/ThymeleafAutoConfiguration.java | 14 +++++++++----- .../velocity/VelocityAutoConfiguration.java | 16 ++++++++++------ .../FreeMarkerAutoConfigurationTests.java | 11 ++++++++--- ...figurationReportLoggingInitializerTests.java | 2 ++ .../ThymeleafAutoConfigurationTests.java | 9 +++++++-- .../VelocityAutoConfigurationTests.java | 9 +++++++-- 11 files changed, 91 insertions(+), 45 deletions(-) diff --git a/spring-boot-actuator/pom.xml b/spring-boot-actuator/pom.xml index b51156efd7..565dd3b486 100644 --- a/spring-boot-actuator/pom.xml +++ b/spring-boot-actuator/pom.xml @@ -261,11 +261,6 @@ true - - com.jayway.jsonpath - json-path - test - org.springframework.boot spring-boot @@ -277,6 +272,11 @@ logback-classic test + + com.jayway.jsonpath + json-path + test + org.apache.tomcat.embed tomcat-embed-logging-juli @@ -317,5 +317,10 @@ spring-security-test test + + org.yaml + snakeyaml + test + diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 3100f1560c..8d8b149f21 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -548,6 +548,11 @@ test-jar test + + ch.qos.logback + logback-classic + test + com.atomikos transactions-jms @@ -568,11 +573,6 @@ hsqldb test - - org.slf4j - slf4j-jdk14 - test - org.springframework spring-test @@ -586,6 +586,7 @@ org.yaml snakeyaml + test diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java index 6251f23294..600d752f8b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java @@ -23,6 +23,8 @@ import java.util.Properties; import javax.annotation.PostConstruct; import javax.servlet.Servlet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -39,7 +41,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean; -import org.springframework.util.Assert; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; @@ -58,6 +59,9 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; @EnableConfigurationProperties(FreeMarkerProperties.class) public class FreeMarkerAutoConfiguration { + private static final Log logger = LogFactory + .getLog(FreeMarkerAutoConfiguration.class); + @Autowired private ApplicationContext applicationContext; @@ -77,10 +81,12 @@ public class FreeMarkerAutoConfiguration { break; } } - Assert.notNull(templatePathLocation, "Cannot find template location(s): " - + locations + " (please add some templates, " - + "check your FreeMarker configuration, or set " - + "spring.freemarker.checkTemplateLocation=false)"); + if (templatePathLocation == null) { + logger.warn("Cannot find template location(s): " + locations + + " (please add some templates, " + + "check your FreeMarker configuration, or set " + + "spring.freemarker.checkTemplateLocation=false)"); + } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index 33f984174e..abfec18921 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -22,6 +22,8 @@ import java.security.ProtectionDomain; import javax.annotation.PostConstruct; import javax.servlet.Servlet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -36,7 +38,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.util.Assert; import org.springframework.web.servlet.view.UrlBasedViewResolver; import org.springframework.web.servlet.view.groovy.GroovyMarkupConfig; import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer; @@ -61,6 +62,9 @@ import groovy.text.markup.MarkupTemplateEngine; @EnableConfigurationProperties(GroovyTemplateProperties.class) public class GroovyTemplateAutoConfiguration { + private static final Log logger = LogFactory + .getLog(GroovyTemplateAutoConfiguration.class); + @Configuration @ConditionalOnClass(GroovyMarkupConfigurer.class) public static class GroovyMarkupConfiguration { @@ -79,11 +83,12 @@ public class GroovyTemplateAutoConfiguration { if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) { TemplateLocation location = new TemplateLocation( this.properties.getResourceLoaderPath()); - Assert.state(location.exists(this.applicationContext), - "Cannot find template location: " + location - + " (please add some templates, check your Groovy " - + "configuration, or set spring.groovy.template." - + "check-template-location=false)"); + if (!location.exists(this.applicationContext)) { + logger.warn("Cannot find template location: " + location + + " (please add some templates, check your Groovy " + + "configuration, or set spring.groovy.template." + + "check-template-location=false)"); + } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java index 82dd5dbab0..53558c8854 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java @@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.mustache; import javax.annotation.PostConstruct; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -31,7 +33,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.env.Environment; -import org.springframework.util.Assert; import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Collector; @@ -49,6 +50,8 @@ import com.samskivert.mustache.Mustache.TemplateLoader; @EnableConfigurationProperties(MustacheProperties.class) public class MustacheAutoConfiguration { + private static final Log logger = LogFactory.getLog(MustacheAutoConfiguration.class); + @Autowired private MustacheProperties mustache; @@ -62,11 +65,12 @@ public class MustacheAutoConfiguration { public void checkTemplateLocationExists() { if (this.mustache.isCheckTemplateLocation()) { TemplateLocation location = new TemplateLocation(this.mustache.getPrefix()); - Assert.state(location.exists(this.applicationContext), - "Cannot find template location: " + location - + " (please add some templates, check your Mustache " - + "configuration, or set spring.mustache." - + "check-template-location=false)"); + if (!location.exists(this.applicationContext)) { + logger.warn("Cannot find template location: " + location + + " (please add some templates, check your Mustache " + + "configuration, or set spring.mustache." + + "check-template-location=false)"); + } } } 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 0f819e05b9..a1c2b74a78 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 @@ -23,6 +23,8 @@ import java.util.LinkedHashMap; import javax.annotation.PostConstruct; import javax.servlet.Servlet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -38,7 +40,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; -import org.springframework.util.Assert; import org.springframework.util.MimeType; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.thymeleaf.dialect.IDialect; @@ -68,6 +69,8 @@ import nz.net.ultraq.thymeleaf.LayoutDialect; @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class ThymeleafAutoConfiguration { + private static final Log logger = LogFactory.getLog(ThymeleafAutoConfiguration.class); + @Configuration @ConditionalOnMissingBean(name = "defaultTemplateResolver") public static class DefaultTemplateResolverConfiguration { @@ -84,10 +87,11 @@ public class ThymeleafAutoConfiguration { if (checkTemplateLocation) { TemplateLocation location = new TemplateLocation( this.properties.getPrefix()); - Assert.state(location.exists(this.applicationContext), - "Cannot find template location: " + location - + " (please add some templates or check " - + "your Thymeleaf configuration)"); + if (!location.exists(this.applicationContext)) { + logger.warn("Cannot find template location: " + location + + " (please add some templates or check " + + "your Thymeleaf configuration)"); + } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java index 7792aa13bf..31953565ff 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java @@ -22,6 +22,8 @@ import java.util.Properties; import javax.annotation.PostConstruct; import javax.servlet.Servlet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.exception.VelocityException; import org.springframework.beans.factory.annotation.Autowired; @@ -42,7 +44,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ui.velocity.VelocityEngineFactory; import org.springframework.ui.velocity.VelocityEngineFactoryBean; -import org.springframework.util.Assert; import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; import org.springframework.web.servlet.view.velocity.VelocityConfig; import org.springframework.web.servlet.view.velocity.VelocityConfigurer; @@ -60,6 +61,8 @@ import org.springframework.web.servlet.view.velocity.VelocityConfigurer; @EnableConfigurationProperties(VelocityProperties.class) public class VelocityAutoConfiguration { + private static final Log logger = LogFactory.getLog(VelocityAutoConfiguration.class); + @Autowired private ApplicationContext applicationContext; @@ -71,11 +74,12 @@ public class VelocityAutoConfiguration { if (this.properties.isCheckTemplateLocation()) { TemplateLocation location = new TemplateLocation( this.properties.getResourceLoaderPath()); - Assert.state(location.exists(this.applicationContext), - "Cannot find template location: " + location - + " (please add some templates, check your Velocity " - + "configuration, or set spring.velocity." - + "checkTemplateLocation=false)"); + if (!location.exists(this.applicationContext)) { + logger.warn("Cannot find template location: " + location + + " (please add some templates, check your Velocity " + + "configuration, or set spring.velocity." + + "checkTemplateLocation=false)"); + } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java index 366c6fb15f..4153a7598f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java @@ -24,9 +24,10 @@ import javax.servlet.http.HttpServletRequest; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.boot.test.OutputCapture; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -52,6 +53,9 @@ import static org.junit.Assert.assertThat; */ public class FreeMarkerAutoConfigurationTests { + @Rule + public OutputCapture output = new OutputCapture(); + private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); @Before @@ -73,10 +77,11 @@ public class FreeMarkerAutoConfigurationTests { assertThat(this.context.getBean(FreeMarkerConfigurer.class), notNullValue()); } - @Test(expected = BeanCreationException.class) - public void nonExistentTemplateLocation() { + @Test + public void nonExistentTemplateLocation() throws Exception { registerAndRefreshContext("spring.freemarker.templateLoaderPath:" + "classpath:/does-not-exist/,classpath:/also-does-not-exist"); + this.output.expect(containsString("Cannot find template location")); } @Test diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java index 12d3f86ae6..10b3179748 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java @@ -204,6 +204,7 @@ public class AutoConfigurationReportLoggingInitializerTests { } public static class MockLogFactory extends LogFactoryImpl { + @Override public Log getInstance(String name) throws LogConfigurationException { if (AutoConfigurationReportLoggingInitializer.class.getName().equals(name)) { @@ -211,6 +212,7 @@ public class AutoConfigurationReportLoggingInitializerTests { } return new NoOpLog(); } + } @Configuration 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 6b19c681e6..5610ae3683 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 @@ -21,10 +21,11 @@ import java.util.Collections; import java.util.Locale; import org.junit.After; +import org.junit.Rule; import org.junit.Test; -import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.boot.test.OutputCapture; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -55,6 +56,9 @@ import static org.junit.Assert.assertTrue; */ public class ThymeleafAutoConfigurationTests { + @Rule + public OutputCapture output = new OutputCapture(); + private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); @After @@ -116,13 +120,14 @@ public class ThymeleafAutoConfigurationTests { assertArrayEquals(new String[] { "foo", "bar" }, views.getViewNames()); } - @Test(expected = BeanCreationException.class) + @Test public void templateLocationDoesNotExist() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "spring.thymeleaf.prefix:classpath:/no-such-directory/"); this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); + this.output.expect(containsString("Cannot find template location")); } @Test diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java index 35ae356185..4a7d0d694d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfigurationTests.java @@ -27,9 +27,10 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.boot.test.OutputCapture; import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; @@ -61,6 +62,9 @@ import static org.junit.Assert.assertThat; */ public class VelocityAutoConfigurationTests { + @Rule + public OutputCapture output = new OutputCapture(); + private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); @Before @@ -82,10 +86,11 @@ public class VelocityAutoConfigurationTests { assertThat(this.context.getBean(VelocityConfigurer.class), notNullValue()); } - @Test(expected = BeanCreationException.class) + @Test public void nonExistentTemplateLocation() { registerAndRefreshContext("spring.velocity.resourceLoaderPath:" + "classpath:/does-not-exist/"); + this.output.expect(containsString("Cannot find template location")); } @Test