From 7787b2ef6a2b8f01319beec08bddc137a2756ec3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 19 May 2014 16:06:45 +0100 Subject: [PATCH] Make Groovy templates work with Groovy 2.2 Fixes gh-890 --- .../GroovyTemplateAutoConfiguration.java | 76 ++++++++++++++----- .../template/GroovyTemplateProperties.java | 9 ++- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index 2223abb108..082cf49b2f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.groovy.template; +import groovy.text.SimpleTemplateEngine; import groovy.text.TemplateEngine; import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.TemplateConfiguration; @@ -27,15 +28,16 @@ import java.util.List; import javax.servlet.Servlet; -import groovy.text.markup.TemplateResolver; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplateViewResolver; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -66,16 +68,14 @@ public class GroovyTemplateAutoConfiguration { @Autowired private GroovyTemplateProperties properties; - @Configuration - @ConditionalOnClass({ Servlet.class, LocaleContextHolder.class }) - @ConditionalOnWebApplication - public static class GroovyWebConfiguration implements BeanClassLoaderAware { + public abstract static class BaseGroovyTemplateConfiguration implements + BeanClassLoaderAware { @Autowired - private ApplicationContext resourceLoader; + private GroovyTemplateProperties properties; @Autowired - private GroovyTemplateProperties properties; + private ApplicationContext resourceLoader; private ClassLoader classLoader = GroovyWebConfiguration.class.getClassLoader(); @@ -84,16 +84,7 @@ public class GroovyTemplateAutoConfiguration { this.classLoader = classLoader; } - @Bean - @ConditionalOnMissingBean(TemplateEngine.class) - public TemplateEngine groovyTemplateEngine() throws Exception { - TemplateConfiguration configuration = this.properties.getConfiguration(); - - return new MarkupTemplateEngine(createParentLoaderForTemplates(), - configuration, new GroovyTemplateResolver()); - } - - private ClassLoader createParentLoaderForTemplates() throws Exception { + protected ClassLoader createParentLoaderForTemplates() throws Exception { Resource[] resources = this.resourceLoader.getResources(this.properties .getPrefix()); if (resources.length > 0) { @@ -111,6 +102,55 @@ public class GroovyTemplateAutoConfiguration { } } + } + + @Configuration + @ConditionalOnClass(MarkupTemplateEngine.class) + public static class GroovyMarkupConfiguration extends BaseGroovyTemplateConfiguration { + + @Autowired + private GroovyTemplateProperties properties; + + @Bean + @ConfigurationProperties(prefix = "spring.groovy.template.configuration") + public TemplateConfiguration groovyTemplateConfiguration() { + return new TemplateConfiguration(); + } + + @Bean + @ConditionalOnMissingBean(TemplateEngine.class) + public TemplateEngine groovyTemplateEngine() throws Exception { + TemplateConfiguration configuration = groovyTemplateConfiguration(); + configuration.setCacheTemplates(this.properties.isCache()); + return new MarkupTemplateEngine(createParentLoaderForTemplates(), + configuration, new GroovyTemplateResolver()); + } + + } + + @Configuration + @ConditionalOnMissingClass(name = "groovy.text.markup.MarkupTemplateEngine") + public static class GroovySimpleConfiguration extends BaseGroovyTemplateConfiguration { + + @Autowired + private GroovyTemplateProperties properties; + + @Bean + @ConditionalOnMissingBean(TemplateEngine.class) + public TemplateEngine groovyTemplateEngine() throws Exception { + return new SimpleTemplateEngine(createParentLoaderForTemplates()); + } + + } + + @Configuration + @ConditionalOnClass({ Servlet.class, LocaleContextHolder.class }) + @ConditionalOnWebApplication + public static class GroovyWebConfiguration { + + @Autowired + private GroovyTemplateProperties properties; + @Bean @ConditionalOnMissingBean(name = "groovyTemplateViewResolver") public GroovyTemplateViewResolver groovyTemplateViewResolver(TemplateEngine engine) { @@ -129,6 +169,6 @@ public class GroovyTemplateAutoConfiguration { return resolver; } - } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateProperties.java index 16345d6270..3ac8c35895 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateProperties.java @@ -16,7 +16,8 @@ package org.springframework.boot.autoconfigure.groovy.template; -import groovy.text.markup.TemplateConfiguration; +import java.util.HashMap; +import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -45,7 +46,7 @@ public class GroovyTemplateProperties { private boolean checkTemplateLocation = false; - private TemplateConfiguration configuration = new TemplateConfiguration(); + private Map configuration = new HashMap(); public void setCheckTemplateLocation(boolean checkTemplateLocation) { this.checkTemplateLocation = checkTemplateLocation; @@ -105,11 +106,11 @@ public class GroovyTemplateProperties { this.suffix = suffix; } - public void setConfiguration(TemplateConfiguration configuration) { + public void setConfiguration(Map configuration) { this.configuration = configuration; } - public TemplateConfiguration getConfiguration() { + public Map getConfiguration() { return this.configuration; }