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 5d9dc80406..35f793b50d 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 @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.freemarker; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; import javax.annotation.PostConstruct; @@ -65,10 +67,18 @@ public class FreeMarkerAutoConfiguration { @PostConstruct public void checkTemplateLocationExists() { if (this.properties.isCheckTemplateLocation()) { - Resource resource = this.resourceLoader.getResource(this.properties - .getTemplateLoaderPath()); - Assert.state(resource.exists(), "Cannot find template location: " + resource - + " (please add some templates, " + Resource templatePathResource = null; + List resources = new ArrayList(); + for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) { + Resource resource = this.resourceLoader.getResource(templateLoaderPath); + resources.add(resource); + if (resource.exists()) { + templatePathResource = resource; + break; + } + } + Assert.notNull(templatePathResource, "Cannot find template location(s): " + + resources + " (please add some templates, " + "check your FreeMarker configuration, or set " + "spring.freemarker.checkTemplateLocation=false)"); } @@ -80,7 +90,7 @@ public class FreeMarkerAutoConfiguration { protected FreeMarkerProperties properties; protected void applyProperties(FreeMarkerConfigurationFactory factory) { - factory.setTemplateLoaderPath(this.properties.getTemplateLoaderPath()); + factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath()); factory.setDefaultEncoding(this.properties.getCharSet()); Properties settings = new Properties(); settings.putAll(this.properties.getSettings()); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java index a10236dd8d..2df1a1b65a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java @@ -40,7 +40,7 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties private Map settings = new HashMap(); - private String templateLoaderPath = DEFAULT_TEMPLATE_LOADER_PATH; + private String[] templateLoaderPath = new String[] { DEFAULT_TEMPLATE_LOADER_PATH }; public FreeMarkerProperties() { super(DEFAULT_PREFIX, DEFAULT_SUFFIX); @@ -54,12 +54,12 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties this.settings = settings; } - public String getTemplateLoaderPath() { + public String[] getTemplateLoaderPath() { return this.templateLoaderPath; } - public void setTemplateLoaderPath(String templateLoaderPath) { - this.templateLoaderPath = templateLoaderPath; + public void setTemplateLoaderPath(String... templateLoaderPaths) { + this.templateLoaderPath = templateLoaderPaths; } } 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 b2a5e99035..0c08ced164 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 @@ -73,7 +73,7 @@ public class FreeMarkerAutoConfigurationTests { @Test(expected = BeanCreationException.class) public void nonExistentTemplateLocation() { registerAndRefreshContext("spring.freemarker.templateLoaderPath:" - + "classpath:/does-not-exist/"); + + "classpath:/does-not-exist/,classpath:/also-does-not-exist"); } @Test @@ -83,6 +83,13 @@ public class FreeMarkerAutoConfigurationTests { + "classpath:/templates/empty-directory/"); } + @Test + public void nonExistentLocationAndEmptyLocation() { + new File("target/test-classes/templates/empty-directory").mkdir(); + registerAndRefreshContext("spring.freemarker.templateLoaderPath:" + + "classpath:/does-not-exist/,classpath:/templates/empty-directory/"); + } + @Test public void defaultViewResolution() throws Exception { registerAndRefreshContext(); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ed6e849507..90fc65906e 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -123,7 +123,7 @@ content into your application; rather pick only the properties that you need. spring.freemarker.requestContextAttribute= spring.freemarker.settings.*= spring.freemarker.suffix=.ftl - spring.freemarker.templateLoaderPath=classpath:/templates/ + spring.freemarker.templateLoaderPath=classpath:/templates/ # comma-separated list spring.freemarker.viewNames= # whitelist of view names that can be resolved # GROOVY TEMPLATES ({sc-spring-boot-autoconfigure}/groovy/template/GroovyTemplateAutoConfiguration.{sc-ext}[GroovyTemplateAutoConfiguration])