Add support for FreeMarker in non-webapp

The existing freemarker support only works in a webapp. This
change adds a FreeMarker Configuration bean (in both web- and
non webapps) so it can be used to load a Template and render it
(e.g. with Spring's FreeMarkerTemplateUtils).

See gh-679
pull/762/head
Dave Syer 11 years ago
parent 3a6d4efe28
commit cb1b3481be

@ -25,8 +25,10 @@ import javax.servlet.Servlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
@ -38,7 +40,9 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
@ -86,10 +90,25 @@ public class FreeMarkerAutoConfiguration {
}
}
}
@Configuration
@ConditionalOnClass(Servlet.class)
@ConditionalOnNotWebApplication
public static class FreeMarkerConfiguration implements EnvironmentAware {
private RelaxedPropertyResolver environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
"spring.freemarker.");
}
@Bean
@ConditionalOnMissingBean(name = "freeMarkerConfigurer")
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
@ConditionalOnMissingBean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurer() {
FreeMarkerConfigurationFactoryBean freeMarkerConfigurer = new FreeMarkerConfigurationFactoryBean();
freeMarkerConfigurer.setTemplateLoaderPath(this.environment.getProperty(
"templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH));
freeMarkerConfigurer.setDefaultEncoding(this.environment.getProperty(
@ -101,6 +120,7 @@ public class FreeMarkerAutoConfiguration {
freeMarkerConfigurer.setFreemarkerSettings(settings);
return freeMarkerConfigurer;
}
}
@Configuration
@ -116,6 +136,30 @@ public class FreeMarkerAutoConfiguration {
"spring.freemarker.");
}
@Bean
@ConditionalOnBean(FreeMarkerConfig.class)
@ConditionalOnMissingBean
public freemarker.template.Configuration freemarkerConfiguration(
FreeMarkerConfig configurer) {
return configurer.getConfiguration();
}
@Bean
@ConditionalOnMissingBean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setTemplateLoaderPath(this.environment.getProperty(
"templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH));
freeMarkerConfigurer.setDefaultEncoding(this.environment.getProperty(
"templateEncoding", "UTF-8"));
Map<String, Object> settingsMap = this.environment
.getSubProperties("settings.");
Properties settings = new Properties();
settings.putAll(settingsMap);
freeMarkerConfigurer.setFreemarkerSettings(settings);
return freeMarkerConfigurer;
}
@Bean
@ConditionalOnMissingBean(name = "freeMarkerViewResolver")
public FreeMarkerViewResolver freeMarkerViewResolver() {

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.freemarker;
import java.io.File;
import java.io.StringWriter;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
@ -186,6 +187,21 @@ public class FreeMarkerAutoConfigurationTests {
.getConfiguration().getSetting("boolean_format"));
}
@Test
public void renderTemplate() throws Exception {
this.context.register(FreeMarkerAutoConfiguration.class);
this.context.refresh();
freemarker.template.Configuration freemarker = this.context
.getBean(freemarker.template.Configuration.class);
StringWriter writer = new StringWriter();
freemarker.getTemplate("message.ftl").process(this, writer);
assertTrue("Wrong content: " + writer, writer.toString().contains("Hello World"));
}
public String getGreeting() {
return "Hello World";
}
private MockHttpServletResponse render(String viewName) throws Exception {
View view = this.context.getBean(FreeMarkerViewResolver.class).resolveViewName(

@ -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 org.springframework.boot.autoconfigure.freemarker;
import java.io.StringWriter;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link FreeMarkerAutoConfiguration}.
*
* @author Andy Wilkinson
*/
public class FreeMarkerNonWebappTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void renderTemplate() throws Exception {
this.context.register(FreeMarkerAutoConfiguration.class);
this.context.refresh();
freemarker.template.Configuration freemarker = this.context
.getBean(freemarker.template.Configuration.class);
StringWriter writer = new StringWriter();
freemarker.getTemplate("message.ftl").process(this, writer);
assertTrue("Wrong content: " + writer, writer.toString().contains("Hello World"));
}
public String getGreeting() {
return "Hello World";
}
}
Loading…
Cancel
Save