From 65684957eabc66203cc67067c6b88060eda7c7d4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 14 Jul 2014 11:08:16 +0200 Subject: [PATCH] Use @ConfigurationProperties for Thymeleaf Fixes gh-1250 --- .../thymeleaf/ThymeleafAutoConfiguration.java | 98 +++++++++++++++---- 1 file changed, 79 insertions(+), 19 deletions(-) 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 2ee9a77a3aa..8705f09e81e 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 @@ -31,6 +31,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -53,6 +55,7 @@ import org.thymeleaf.templateresolver.TemplateResolver; * * @author Dave Syer * @author Andy Wilkinson + * @author Stephane Nicoll */ @Configuration @ConditionalOnClass(SpringTemplateEngine.class) @@ -64,27 +67,22 @@ public class ThymeleafAutoConfiguration { public static final String DEFAULT_SUFFIX = ".html"; @Configuration + @EnableConfigurationProperties(ThymeleafProperties.class) @ConditionalOnMissingBean(name = "defaultTemplateResolver") - public static class DefaultTemplateResolverConfiguration implements EnvironmentAware { + public static class DefaultTemplateResolverConfiguration { + + @Autowired + private ThymeleafProperties properties; @Autowired private final ResourceLoader resourceLoader = new DefaultResourceLoader(); - private RelaxedPropertyResolver environment; - - @Override - public void setEnvironment(Environment environment) { - this.environment = new RelaxedPropertyResolver(environment, - "spring.thymeleaf."); - } @PostConstruct public void checkTemplateLocationExists() { - Boolean checkTemplateLocation = this.environment.getProperty( - "checkTemplateLocation", Boolean.class, true); + Boolean checkTemplateLocation = this.properties.isCheckTemplateLocation(); if (checkTemplateLocation) { - Resource resource = this.resourceLoader.getResource(this.environment - .getProperty("prefix", DEFAULT_PREFIX)); + Resource resource = this.resourceLoader.getResource(this.properties.getPrefix()); Assert.state(resource.exists(), "Cannot find template location: " + resource + " (please add some templates " + "or check your Thymeleaf configuration)"); @@ -95,13 +93,11 @@ public class ThymeleafAutoConfiguration { public ITemplateResolver defaultTemplateResolver() { TemplateResolver resolver = new TemplateResolver(); resolver.setResourceResolver(thymeleafResourceResolver()); - resolver.setPrefix(this.environment.getProperty("prefix", DEFAULT_PREFIX)); - resolver.setSuffix(this.environment.getProperty("suffix", DEFAULT_SUFFIX)); - resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5")); - resolver.setCharacterEncoding(this.environment.getProperty("encoding", - "UTF-8")); - resolver.setCacheable(this.environment.getProperty("cache", Boolean.class, - true)); + resolver.setPrefix(this.properties.getPrefix()); + resolver.setSuffix(this.properties.getSuffix()); + resolver.setTemplateMode(this.properties.getMode()); + resolver.setCharacterEncoding(this.properties.getEncoding()); + resolver.setCacheable(this.properties.isCache()); return resolver; } @@ -111,6 +107,70 @@ public class ThymeleafAutoConfiguration { } } + @ConfigurationProperties("spring.thymeleaf") + public static class ThymeleafProperties { + + private boolean checkTemplateLocation = true; + + private String prefix = DEFAULT_PREFIX; + + private String suffix = DEFAULT_SUFFIX; + + private String mode = "HTML5"; + + private String encoding = "UTF-8"; + + private boolean cache = true; + + public boolean isCheckTemplateLocation() { + return checkTemplateLocation; + } + + public void setCheckTemplateLocation(boolean checkTemplateLocation) { + this.checkTemplateLocation = checkTemplateLocation; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getSuffix() { + return suffix; + } + + public void setSuffix(String suffix) { + this.suffix = suffix; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = mode; + } + + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public boolean isCache() { + return cache; + } + + public void setCache(boolean cache) { + this.cache = cache; + } + } + @Configuration @ConditionalOnMissingBean(SpringTemplateEngine.class) protected static class ThymeleafDefaultConfiguration {