Use @ConfigurationProperties for Thymeleaf

Fixes gh-1250
This commit is contained in:
Stephane Nicoll 2014-07-14 11:08:16 +02:00
parent 90cf722365
commit 65684957ea
1 changed files with 79 additions and 19 deletions

View File

@ -31,6 +31,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver; 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.EnvironmentAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -53,6 +55,7 @@ import org.thymeleaf.templateresolver.TemplateResolver;
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
@Configuration @Configuration
@ConditionalOnClass(SpringTemplateEngine.class) @ConditionalOnClass(SpringTemplateEngine.class)
@ -64,27 +67,22 @@ public class ThymeleafAutoConfiguration {
public static final String DEFAULT_SUFFIX = ".html"; public static final String DEFAULT_SUFFIX = ".html";
@Configuration @Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnMissingBean(name = "defaultTemplateResolver") @ConditionalOnMissingBean(name = "defaultTemplateResolver")
public static class DefaultTemplateResolverConfiguration implements EnvironmentAware { public static class DefaultTemplateResolverConfiguration {
@Autowired
private ThymeleafProperties properties;
@Autowired @Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private final ResourceLoader resourceLoader = new DefaultResourceLoader();
private RelaxedPropertyResolver environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
"spring.thymeleaf.");
}
@PostConstruct @PostConstruct
public void checkTemplateLocationExists() { public void checkTemplateLocationExists() {
Boolean checkTemplateLocation = this.environment.getProperty( Boolean checkTemplateLocation = this.properties.isCheckTemplateLocation();
"checkTemplateLocation", Boolean.class, true);
if (checkTemplateLocation) { if (checkTemplateLocation) {
Resource resource = this.resourceLoader.getResource(this.environment Resource resource = this.resourceLoader.getResource(this.properties.getPrefix());
.getProperty("prefix", DEFAULT_PREFIX));
Assert.state(resource.exists(), "Cannot find template location: " Assert.state(resource.exists(), "Cannot find template location: "
+ resource + " (please add some templates " + resource + " (please add some templates "
+ "or check your Thymeleaf configuration)"); + "or check your Thymeleaf configuration)");
@ -95,13 +93,11 @@ public class ThymeleafAutoConfiguration {
public ITemplateResolver defaultTemplateResolver() { public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new TemplateResolver(); TemplateResolver resolver = new TemplateResolver();
resolver.setResourceResolver(thymeleafResourceResolver()); resolver.setResourceResolver(thymeleafResourceResolver());
resolver.setPrefix(this.environment.getProperty("prefix", DEFAULT_PREFIX)); resolver.setPrefix(this.properties.getPrefix());
resolver.setSuffix(this.environment.getProperty("suffix", DEFAULT_SUFFIX)); resolver.setSuffix(this.properties.getSuffix());
resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5")); resolver.setTemplateMode(this.properties.getMode());
resolver.setCharacterEncoding(this.environment.getProperty("encoding", resolver.setCharacterEncoding(this.properties.getEncoding());
"UTF-8")); resolver.setCacheable(this.properties.isCache());
resolver.setCacheable(this.environment.getProperty("cache", Boolean.class,
true));
return resolver; 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 @Configuration
@ConditionalOnMissingBean(SpringTemplateEngine.class) @ConditionalOnMissingBean(SpringTemplateEngine.class)
protected static class ThymeleafDefaultConfiguration { protected static class ThymeleafDefaultConfiguration {