diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java index 2e7359c77e1..a10236dd8de 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerProperties.java @@ -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); - } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/template/AbstractTemplateViewResolverProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/template/AbstractTemplateViewResolverProperties.java index 496605dd659..95f35acd17c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/template/AbstractTemplateViewResolverProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/template/AbstractTemplateViewResolverProperties.java @@ -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); + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java index 627aa41f4d3..1343a5b5a5f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java @@ -118,7 +118,7 @@ public class VelocityAutoConfiguration { @Bean public VelocityEngine velocityEngine(VelocityConfigurer configurer) throws VelocityException, IOException { - return configurer.createVelocityEngine(); + return configurer.getVelocityEngine(); } @Bean diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityProperties.java index 22b2b19e50f..020034f3b38 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityProperties.java @@ -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()); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 321ce3cb8f4..ae9e138371c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -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); } diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 091a8835b1a..34c49750d9a 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -48,6 +48,7 @@ spring-boot-sample-tomcat-multi-connectors spring-boot-sample-tomcat8-jsp spring-boot-sample-traditional + spring-boot-sample-velocity spring-boot-sample-web-freemarker spring-boot-sample-web-groovy-templates spring-boot-sample-web-method-security diff --git a/spring-boot-samples/spring-boot-sample-velocity/pom.xml b/spring-boot-samples/spring-boot-sample-velocity/pom.xml new file mode 100644 index 00000000000..7c2fe2fb5ed --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-velocity/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 1.1.6.BUILD-SNAPSHOT + + spring-boot-sample-velocity + spring-boot-sample-velocity + Spring Boot Web Velocity Sample + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + / + + + + org.springframework.boot + spring-boot-starter + + + org.springframework + spring-context-support + + + org.apache.velocity + velocity + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + false + + + + + diff --git a/spring-boot-samples/spring-boot-sample-velocity/src/main/java/sample/velocity/SampleVelocityApplication.java b/spring-boot-samples/spring-boot-sample-velocity/src/main/java/sample/velocity/SampleVelocityApplication.java new file mode 100644 index 00000000000..497825838ba --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-velocity/src/main/java/sample/velocity/SampleVelocityApplication.java @@ -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 model = new HashMap(); + 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); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/application.properties new file mode 100644 index 00000000000..e504f79eb9c --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/application.properties @@ -0,0 +1 @@ +application.message: Hello, Andy diff --git a/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/templates/welcome.vm b/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/templates/welcome.vm new file mode 100644 index 00000000000..7560db959d0 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-velocity/src/main/resources/templates/welcome.vm @@ -0,0 +1,2 @@ +From application: $time +Message: $message \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-velocity/src/test/java/sample/velocity/SampleVelocityApplicationTests.java b/spring-boot-samples/spring-boot-sample-velocity/src/test/java/sample/velocity/SampleVelocityApplicationTests.java new file mode 100644 index 00000000000..ad325f46b3d --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-velocity/src/test/java/sample/velocity/SampleVelocityApplicationTests.java @@ -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")); + } + +}