diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java index d4c33b6d12..b0f877a25d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java @@ -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 settingsMap = this.environment + .getSubProperties("settings."); + Properties settings = new Properties(); + settings.putAll(settingsMap); + freeMarkerConfigurer.setFreemarkerSettings(settings); + return freeMarkerConfigurer; + } + @Bean @ConditionalOnMissingBean(name = "freeMarkerViewResolver") public FreeMarkerViewResolver freeMarkerViewResolver() { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java index 976ef5177c..38be24babb 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java @@ -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( diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerNonWebappTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerNonWebappTests.java new file mode 100644 index 0000000000..78fcb2d971 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerNonWebappTests.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 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"; + } +} diff --git a/spring-boot-autoconfigure/src/test/resources/templates/message.ftl b/spring-boot-autoconfigure/src/test/resources/templates/message.ftl new file mode 100644 index 0000000000..3908877ab6 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/templates/message.ftl @@ -0,0 +1 @@ +Message: ${greeting} \ No newline at end of file