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
pull/4087/head
Phillip Webb 9 years ago
parent 5ccd9afc78
commit 5e58645202

@ -261,11 +261,6 @@
<optional>true</optional>
</dependency>
<!-- Test -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
@ -277,6 +272,11 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
@ -317,5 +317,10 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -548,6 +548,11 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jms</artifactId>
@ -568,11 +573,6 @@
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@ -586,6 +586,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -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,12 +81,14 @@ public class FreeMarkerAutoConfiguration {
break;
}
}
Assert.notNull(templatePathLocation, "Cannot find template location(s): "
+ locations + " (please add some templates, "
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)");
}
}
}
protected static class FreeMarkerConfiguration {

@ -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,13 +83,14 @@ 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
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)");
}
}
}
/**
* MarkupTemplateEngine could be loaded from groovy-templates or groovy-all.

@ -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,13 +65,14 @@ 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
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)");
}
}
}
@Bean
@ConditionalOnMissingBean(Mustache.Compiler.class)

@ -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,12 +87,13 @@ public class ThymeleafAutoConfiguration {
if (checkTemplateLocation) {
TemplateLocation location = new TemplateLocation(
this.properties.getPrefix());
Assert.state(location.exists(this.applicationContext),
"Cannot find template location: " + location
if (!location.exists(this.applicationContext)) {
logger.warn("Cannot find template location: " + location
+ " (please add some templates or check "
+ "your Thymeleaf configuration)");
}
}
}
@Bean
public TemplateResolver defaultTemplateResolver() {

@ -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,13 +74,14 @@ 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
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)");
}
}
}
protected static class VelocityConfiguration {

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save