Add indirection to avoid runtime dependency on MVC in templates

Velocity and Freemarker share some common properties so the base class for
configuring their properties makes some sense. Unfortunately the implementation
pulls in Spring MVC at runtime because of the signature of one method (that
would never be called). We can fix that in a number of ways, but the least
disruptive is probably to change the signature of that method and only refer
to the concrete template view resolver type if the method is called.

Fixes gh-1437
This commit is contained in:
Dave Syer
2014-08-26 08:52:24 +01:00
parent f4dc090bae
commit 95d65c2ff5
11 changed files with 183 additions and 18 deletions

View File

@@ -21,7 +21,6 @@ import java.util.Map;
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
/**
* {@link ConfigurationProperties} for configuring FreeMarker
@@ -63,11 +62,4 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
this.templateLoaderPath = templateLoaderPath;
}
/**
* Apply the given properties to a {@link FreeMarkerViewResolver}.
* @param resolver the resolver to apply the properties to.
*/
public void applyToViewResolver(FreeMarkerViewResolver resolver) {
super.applyToViewResolver(resolver);
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.template;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
/**
@@ -158,10 +159,18 @@ public abstract class AbstractTemplateViewResolverProperties {
}
/**
* Apply the given properties to a {@link AbstractTemplateViewResolver}.
* @param resolver the resolver to apply the properties to.
* Apply the given properties to a {@link AbstractTemplateViewResolver}. Use Object in
* signature to avoid runtime dependency on MVC, which means that the template engine
* can be used in a non-web application.
*
* @param viewResolver the resolver to apply the properties to.
*/
protected void applyToViewResolver(AbstractTemplateViewResolver resolver) {
public void applyToViewResolver(Object viewResolver) {
Assert.isInstanceOf(AbstractTemplateViewResolver.class, viewResolver,
"ViewResolver is not an instance of AbstractTemplateViewResolver :"
+ viewResolver);
AbstractTemplateViewResolver resolver = (AbstractTemplateViewResolver) viewResolver;
resolver.setPrefix(getPrefix());
resolver.setSuffix(getSuffix());
resolver.setCache(isCache());
@@ -175,6 +184,7 @@ public abstract class AbstractTemplateViewResolverProperties {
// The resolver usually acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
}
}

View File

@@ -118,7 +118,7 @@ public class VelocityAutoConfiguration {
@Bean
public VelocityEngine velocityEngine(VelocityConfigurer configurer)
throws VelocityException, IOException {
return configurer.createVelocityEngine();
return configurer.getVelocityEngine();
}
@Bean

View File

@@ -92,12 +92,10 @@ public class VelocityProperties extends AbstractTemplateViewResolverProperties {
this.toolboxConfigLocation = toolboxConfigLocation;
}
/**
* Apply the given properties to a {@link VelocityViewResolver}.
* @param resolver the resolver to apply the properties to.
*/
public void applyToViewResolver(VelocityViewResolver resolver) {
super.applyToViewResolver(resolver);
@Override
public void applyToViewResolver(Object viewResolver) {
super.applyToViewResolver(viewResolver);
VelocityViewResolver resolver = (VelocityViewResolver) viewResolver;
resolver.setToolboxConfigLocation(getToolboxConfigLocation());
resolver.setDateToolAttribute(getDateToolAttribute());
resolver.setNumberToolAttribute(getNumberToolAttribute());

View File

@@ -275,6 +275,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
public void customize(Connector connector) {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
@SuppressWarnings("rawtypes")
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMaxThreads(Tomcat.this.maxThreads);
}
@@ -288,6 +289,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
public void customize(Connector connector) {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) {
@SuppressWarnings("rawtypes")
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
protocol.setMaxHttpHeaderSize(Tomcat.this.maxHttpHeaderSize);
}