Make Groovy templates work with Groovy 2.2

Fixes gh-890
pull/931/head
Dave Syer 11 years ago
parent e964b9eb34
commit 7787b2ef6a

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

@ -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<String, Object> configuration = new HashMap<String, Object>();
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<String, Object> configuration) {
this.configuration = configuration;
}
public TemplateConfiguration getConfiguration() {
public Map<String, Object> getConfiguration() {
return this.configuration;
}

Loading…
Cancel
Save