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);
}

View File

@ -48,6 +48,7 @@
<module>spring-boot-sample-tomcat-multi-connectors</module>
<module>spring-boot-sample-tomcat8-jsp</module>
<module>spring-boot-sample-traditional</module>
<module>spring-boot-sample-velocity</module>
<module>spring-boot-sample-web-freemarker</module>
<module>spring-boot-sample-web-groovy-templates</module>
<module>spring-boot-sample-web-method-security</module>

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.1.6.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-velocity</artifactId>
<name>spring-boot-sample-velocity</name>
<description>Spring Boot Web Velocity Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,57 @@
/*
* Copyright 2012-2014 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 sample.velocity;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.velocity.VelocityEngineUtils;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleVelocityApplication implements CommandLineRunner {
@Value("${application.message}")
private String message;
@Autowired
private VelocityEngine engine;
@Override
public void run(String... args) throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("time", new Date());
model.put("message", this.message);
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(this.engine,
"welcome.vm", "UTF-8", model));
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleVelocityApplication.class, args);
}
}

View File

@ -0,0 +1 @@
application.message: Hello, Andy

View File

@ -0,0 +1,2 @@
From application: $time
Message: $message

View File

@ -0,0 +1,46 @@
/*
* Copyright 2012-2014 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 sample.velocity;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.OutputCapture;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
/**
* Basic integration tests for Velocity application with no web layer.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleVelocityApplication.class)
public class SampleVelocityApplicationTests {
@ClassRule
public static OutputCapture output = new OutputCapture();
@Test
public void testVelocityTemplate() throws Exception {
String result = SampleVelocityApplicationTests.output.toString();
assertTrue("Wrong output: " + result, result.contains("Hello, Andy"));
}
}