Add FreeMarkerProperties instead of raw Environment access
It's better for readability and tooling. Changed templateLoaderPath -> path (simpler and unlikely to clash)
This commit is contained in:
parent
6a10df0933
commit
d2112e27a3
|
|
@ -29,12 +29,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
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;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
|
@ -49,65 +47,45 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||||
* {@link EnableAutoConfiguration Auto-configuration} for FreeMarker.
|
* {@link EnableAutoConfiguration Auto-configuration} for FreeMarker.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Dave Syer
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(freemarker.template.Configuration.class)
|
@ConditionalOnClass(freemarker.template.Configuration.class)
|
||||||
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
||||||
public class FreeMarkerAutoConfiguration implements EnvironmentAware {
|
@EnableConfigurationProperties(FreeMarkerProperties.class)
|
||||||
|
public class FreeMarkerAutoConfiguration {
|
||||||
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
|
|
||||||
|
|
||||||
public static final String DEFAULT_PREFIX = "";
|
|
||||||
|
|
||||||
public static final String DEFAULT_SUFFIX = ".ftl";
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
|
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||||
|
|
||||||
private RelaxedPropertyResolver environment;
|
@Autowired
|
||||||
|
private FreeMarkerProperties properties;
|
||||||
@Override
|
|
||||||
public void setEnvironment(Environment environment) {
|
|
||||||
this.environment = new RelaxedPropertyResolver(environment, "spring.freemarker.");
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void checkTemplateLocationExists() {
|
public void checkTemplateLocationExists() {
|
||||||
Boolean checkTemplateLocation = this.environment.getProperty(
|
if (this.properties.isCheckTemplateLocation()) {
|
||||||
"checkTemplateLocation", Boolean.class, true);
|
Resource resource = this.resourceLoader
|
||||||
if (checkTemplateLocation) {
|
.getResource(this.properties.getPath());
|
||||||
Resource resource = this.resourceLoader.getResource(this.environment
|
|
||||||
.getProperty("templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH));
|
|
||||||
Assert.state(resource.exists(), "Cannot find template location: " + resource
|
Assert.state(resource.exists(), "Cannot find template location: " + resource
|
||||||
+ " (please add some templates "
|
+ " (please add some templates "
|
||||||
+ "or check your FreeMarker configuration)");
|
+ "or check your FreeMarker configuration)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static class FreeMarkerConfiguration implements EnvironmentAware {
|
protected static class FreeMarkerConfiguration {
|
||||||
|
|
||||||
private RelaxedPropertyResolver properties;
|
@Autowired
|
||||||
|
protected FreeMarkerProperties properties;
|
||||||
@Override
|
|
||||||
public void setEnvironment(Environment environment) {
|
|
||||||
this.properties = new RelaxedPropertyResolver(environment,
|
|
||||||
"spring.freemarker.");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void applyProperties(FreeMarkerConfigurationFactory factory) {
|
protected void applyProperties(FreeMarkerConfigurationFactory factory) {
|
||||||
factory.setTemplateLoaderPath(this.properties.getProperty(
|
factory.setTemplateLoaderPath(this.properties.getPath());
|
||||||
"templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH));
|
factory.setDefaultEncoding(this.properties.getCharSet());
|
||||||
factory.setDefaultEncoding(this.properties.getProperty("templateEncoding",
|
|
||||||
"UTF-8"));
|
|
||||||
Properties settings = new Properties();
|
Properties settings = new Properties();
|
||||||
settings.putAll(this.properties.getSubProperties("settings."));
|
settings.putAll(this.properties.getSettings());
|
||||||
factory.setFreemarkerSettings(settings);
|
factory.setFreemarkerSettings(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final RelaxedPropertyResolver getProperties() {
|
|
||||||
return this.properties;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
@ -147,24 +125,20 @@ public class FreeMarkerAutoConfiguration implements EnvironmentAware {
|
||||||
@ConditionalOnMissingBean(name = "freeMarkerViewResolver")
|
@ConditionalOnMissingBean(name = "freeMarkerViewResolver")
|
||||||
public FreeMarkerViewResolver freeMarkerViewResolver() {
|
public FreeMarkerViewResolver freeMarkerViewResolver() {
|
||||||
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
|
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
|
||||||
RelaxedPropertyResolver properties = getProperties();
|
resolver.setPrefix(this.properties.getPrefix());
|
||||||
resolver.setPrefix(properties.getProperty("prefix", DEFAULT_PREFIX));
|
resolver.setSuffix(this.properties.getSuffix());
|
||||||
resolver.setSuffix(properties.getProperty("suffix", DEFAULT_SUFFIX));
|
resolver.setCache(this.properties.isCache());
|
||||||
resolver.setCache(properties.getProperty("cache", Boolean.class, true));
|
resolver.setContentType(this.properties.getContentType());
|
||||||
resolver.setContentType(properties.getProperty("contentType", "text/html"));
|
resolver.setViewNames(this.properties.getViewNames());
|
||||||
resolver.setViewNames(properties.getProperty("viewNames", String[].class));
|
resolver.setExposeRequestAttributes(this.properties
|
||||||
resolver.setExposeRequestAttributes(properties.getProperty(
|
.isExposeRequestAttributes());
|
||||||
"exposeRequestAttributes", Boolean.class, false));
|
resolver.setExposeRequestAttributes(this.properties.isAllowRequestOverride());
|
||||||
resolver.setAllowRequestOverride(properties.getProperty(
|
resolver.setExposeRequestAttributes(this.properties
|
||||||
"allowRequestOverride", Boolean.class, false));
|
.isExposeSessionAttributes());
|
||||||
resolver.setExposeSessionAttributes(properties.getProperty(
|
resolver.setExposeRequestAttributes(this.properties
|
||||||
"exposeSessionAttributes", Boolean.class, false));
|
.isExposeSpringMacroHelpers());
|
||||||
resolver.setAllowSessionOverride(properties.getProperty(
|
resolver.setRequestContextAttribute(this.properties
|
||||||
"allowSessionOverride", Boolean.class, false));
|
.getRequestContextAttribute());
|
||||||
resolver.setExposeSpringMacroHelpers(properties.getProperty(
|
|
||||||
"exposeSpringMacroHelpers", Boolean.class, true));
|
|
||||||
resolver.setRequestContextAttribute(properties
|
|
||||||
.getProperty("requestContextAttribute"));
|
|
||||||
|
|
||||||
// This resolver acts as a fallback resolver (e.g. like a
|
// This resolver acts as a fallback resolver (e.g. like a
|
||||||
// InternalResourceViewResolver) so it needs to have low precedence
|
// InternalResourceViewResolver) so it needs to have low precedence
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,180 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2013 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.freemarker;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
* @since 1.1.0
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties(prefix = "spring.freemarker")
|
||||||
|
public class FreeMarkerProperties {
|
||||||
|
|
||||||
|
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
|
||||||
|
|
||||||
|
public static final String DEFAULT_PREFIX = "";
|
||||||
|
|
||||||
|
public static final String DEFAULT_SUFFIX = ".ftl";
|
||||||
|
|
||||||
|
private String prefix = DEFAULT_PREFIX;
|
||||||
|
|
||||||
|
private String suffix = DEFAULT_SUFFIX;
|
||||||
|
|
||||||
|
private String path = DEFAULT_TEMPLATE_LOADER_PATH;
|
||||||
|
|
||||||
|
private boolean cache;
|
||||||
|
|
||||||
|
private String contentType = "text/html";
|
||||||
|
|
||||||
|
private String charSet = "UTF-8";
|
||||||
|
|
||||||
|
private String[] viewNames;
|
||||||
|
|
||||||
|
private boolean checkTemplateLocation = true;
|
||||||
|
|
||||||
|
private String requestContextAttribute;
|
||||||
|
|
||||||
|
private boolean exposeRequestAttributes = false;
|
||||||
|
|
||||||
|
private boolean exposeSessionAttributes = false;
|
||||||
|
|
||||||
|
private boolean allowRequestOverride = false;
|
||||||
|
|
||||||
|
private boolean exposeSpringMacroHelpers = true;
|
||||||
|
|
||||||
|
private Map<String, String> settings = new HashMap<String, String>();
|
||||||
|
|
||||||
|
public void setCheckTemplateLocation(boolean checkTemplateLocation) {
|
||||||
|
this.checkTemplateLocation = checkTemplateLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCheckTemplateLocation() {
|
||||||
|
return this.checkTemplateLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getViewNames() {
|
||||||
|
return this.viewNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setViewNames(String[] viewNames) {
|
||||||
|
this.viewNames = viewNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCache() {
|
||||||
|
return this.cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCache(boolean cache) {
|
||||||
|
this.cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContentType() {
|
||||||
|
return this.contentType
|
||||||
|
+ (this.contentType.contains(";charset=") ? "" : ";charset="
|
||||||
|
+ this.charSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContentType(String contentType) {
|
||||||
|
this.contentType = contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCharSet() {
|
||||||
|
return this.charSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCharSet(String charSet) {
|
||||||
|
this.charSet = charSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefix() {
|
||||||
|
return this.prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrefix(String prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSuffix() {
|
||||||
|
return this.suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSuffix(String suffix) {
|
||||||
|
this.suffix = suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return this.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRequestContextAttribute() {
|
||||||
|
return this.requestContextAttribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequestContextAttribute(String requestContextAttribute) {
|
||||||
|
this.requestContextAttribute = requestContextAttribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExposeRequestAttributes() {
|
||||||
|
return this.exposeRequestAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
|
||||||
|
this.exposeRequestAttributes = exposeRequestAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExposeSessionAttributes() {
|
||||||
|
return this.exposeSessionAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
|
||||||
|
this.exposeSessionAttributes = exposeSessionAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAllowRequestOverride() {
|
||||||
|
return this.allowRequestOverride;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAllowRequestOverride(boolean allowRequestOverride) {
|
||||||
|
this.allowRequestOverride = allowRequestOverride;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExposeSpringMacroHelpers() {
|
||||||
|
return this.exposeSpringMacroHelpers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
|
||||||
|
this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getSettings() {
|
||||||
|
return this.settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSettings(Map<String, String> settings) {
|
||||||
|
this.settings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -35,13 +35,12 @@ public class FreeMarkerTemplateAvailabilityProvider implements
|
||||||
public boolean isTemplateAvailable(String view, Environment environment,
|
public boolean isTemplateAvailable(String view, Environment environment,
|
||||||
ClassLoader classLoader, ResourceLoader resourceLoader) {
|
ClassLoader classLoader, ResourceLoader resourceLoader) {
|
||||||
if (ClassUtils.isPresent("freemarker.template.Configuration", classLoader)) {
|
if (ClassUtils.isPresent("freemarker.template.Configuration", classLoader)) {
|
||||||
String loaderPath = environment.getProperty(
|
String loaderPath = environment.getProperty("spring.freemarker.path",
|
||||||
"spring.freemarker.templateLoaderPath",
|
FreeMarkerProperties.DEFAULT_TEMPLATE_LOADER_PATH);
|
||||||
FreeMarkerAutoConfiguration.DEFAULT_TEMPLATE_LOADER_PATH);
|
|
||||||
String prefix = environment.getProperty("spring.freemarker.prefix",
|
String prefix = environment.getProperty("spring.freemarker.prefix",
|
||||||
FreeMarkerAutoConfiguration.DEFAULT_PREFIX);
|
FreeMarkerProperties.DEFAULT_PREFIX);
|
||||||
String suffix = environment.getProperty("spring.freemarker.suffix",
|
String suffix = environment.getProperty("spring.freemarker.suffix",
|
||||||
FreeMarkerAutoConfiguration.DEFAULT_SUFFIX);
|
FreeMarkerProperties.DEFAULT_SUFFIX);
|
||||||
return resourceLoader.getResource(loaderPath + prefix + view + suffix)
|
return resourceLoader.getResource(loaderPath + prefix + view + suffix)
|
||||||
.exists();
|
.exists();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,14 +72,14 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test(expected = BeanCreationException.class)
|
||||||
public void nonExistentTemplateLocation() {
|
public void nonExistentTemplateLocation() {
|
||||||
registerAndRefreshContext("spring.freemarker.templateLoaderPath:"
|
registerAndRefreshContext("spring.freemarker.path:"
|
||||||
+ "classpath:/does-not-exist/");
|
+ "classpath:/does-not-exist/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void emptyTemplateLocation() {
|
public void emptyTemplateLocation() {
|
||||||
new File("target/test-classes/templates/empty-directory").mkdir();
|
new File("target/test-classes/templates/empty-directory").mkdir();
|
||||||
registerAndRefreshContext("spring.freemarker.templateLoaderPath:"
|
registerAndRefreshContext("spring.freemarker.path:"
|
||||||
+ "classpath:/templates/empty-directory/");
|
+ "classpath:/templates/empty-directory/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +89,7 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
MockHttpServletResponse response = render("home");
|
MockHttpServletResponse response = render("home");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
assertThat(result, containsString("home"));
|
assertThat(result, containsString("home"));
|
||||||
assertThat(response.getContentType(), equalTo("text/html"));
|
assertThat(response.getContentType(), equalTo("text/html;charset=UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -98,7 +98,7 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
MockHttpServletResponse response = render("home");
|
MockHttpServletResponse response = render("home");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
assertThat(result, containsString("home"));
|
assertThat(result, containsString("home"));
|
||||||
assertThat(response.getContentType(), equalTo("application/json"));
|
assertThat(response.getContentType(), equalTo("application/json;charset=UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -119,7 +119,7 @@ public class FreeMarkerAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customTemplateLoaderPath() throws Exception {
|
public void customTemplateLoaderPath() throws Exception {
|
||||||
registerAndRefreshContext("spring.freemarker.templateLoaderPath:classpath:/custom-templates/");
|
registerAndRefreshContext("spring.freemarker.path:classpath:/custom-templates/");
|
||||||
MockHttpServletResponse response = render("custom");
|
MockHttpServletResponse response = render("custom");
|
||||||
String result = response.getContentAsString();
|
String result = response.getContentAsString();
|
||||||
assertThat(result, containsString("custom"));
|
assertThat(result, containsString("custom"));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue