From 2fe256a8aab135a3548de1482d34aa727dbcea5b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 1 May 2014 21:21:00 +0100 Subject: [PATCH] Polish --- .../FreeMarkerAutoConfiguration.java | 62 +++++++++---------- 1 file changed, 28 insertions(+), 34 deletions(-) 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 06fc05ff3c2..d25b322d64d 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 @@ -55,7 +55,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; @Configuration @ConditionalOnClass(freemarker.template.Configuration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) -public class FreeMarkerAutoConfiguration { +public class FreeMarkerAutoConfiguration implements EnvironmentAware { public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/"; @@ -63,38 +63,32 @@ public class FreeMarkerAutoConfiguration { public static final String DEFAULT_SUFFIX = ".ftl"; - @Configuration - public static class FreeMarkerConfigurerConfiguration implements EnvironmentAware { + @Autowired + private final ResourceLoader resourceLoader = new DefaultResourceLoader(); - @Autowired - private final ResourceLoader resourceLoader = new DefaultResourceLoader(); + private RelaxedPropertyResolver environment; - private RelaxedPropertyResolver environment; + @Override + public void setEnvironment(Environment environment) { + this.environment = new RelaxedPropertyResolver(environment, "spring.freeMarker."); + } - @Override - public void setEnvironment(Environment environment) { - this.environment = new RelaxedPropertyResolver(environment, - "spring.freeMarker."); + @PostConstruct + public void checkTemplateLocationExists() { + Boolean checkTemplateLocation = this.environment.getProperty( + "checkTemplateLocation", Boolean.class, true); + if (checkTemplateLocation) { + Resource resource = this.resourceLoader.getResource(this.environment + .getProperty("templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH)); + Assert.state(resource.exists(), "Cannot find template location: " + resource + + " (please add some templates " + + "or check your FreeMarker configuration)"); } - - @PostConstruct - public void checkTemplateLocationExists() { - Boolean checkTemplateLocation = this.environment.getProperty( - "checkTemplateLocation", Boolean.class, true); - if (checkTemplateLocation) { - Resource resource = this.resourceLoader.getResource(this.environment - .getProperty("templateLoaderPath", DEFAULT_TEMPLATE_LOADER_PATH)); - Assert.state(resource.exists(), "Cannot find template location: " - + resource + " (please add some templates " - + "or check your FreeMarker configuration)"); - } - } - } @Configuration @ConditionalOnNotWebApplication - public static class FreeMarkerConfiguration implements EnvironmentAware { + public static class FreeMarkerNonWebConfiguration implements EnvironmentAware { private RelaxedPropertyResolver environment; @@ -125,7 +119,7 @@ public class FreeMarkerAutoConfiguration { @Configuration @ConditionalOnClass(Servlet.class) @ConditionalOnWebApplication - public static class FreeMarkerViewResolverConfiguration implements EnvironmentAware { + public static class FreeMarkerWebConfiguration implements EnvironmentAware { private RelaxedPropertyResolver environment; @@ -135,14 +129,6 @@ public class FreeMarkerAutoConfiguration { "spring.freemarker."); } - @Bean - @ConditionalOnBean(FreeMarkerConfigurer.class) - @ConditionalOnMissingBean - public freemarker.template.Configuration freemarkerConfiguration( - FreeMarkerConfig configurer) { - return configurer.getConfiguration(); - } - @Bean @ConditionalOnMissingBean public FreeMarkerConfigurer freeMarkerConfigurer() { @@ -159,6 +145,14 @@ public class FreeMarkerAutoConfiguration { return freeMarkerConfigurer; } + @Bean + @ConditionalOnBean(FreeMarkerConfigurer.class) + @ConditionalOnMissingBean + public freemarker.template.Configuration freemarkerConfiguration( + FreeMarkerConfig configurer) { + return configurer.getConfiguration(); + } + @Bean @ConditionalOnMissingBean(name = "freeMarkerViewResolver") public FreeMarkerViewResolver freeMarkerViewResolver() {