From 2041a79970e519d647913ee926d80646ed6f695b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 22 Aug 2013 01:47:08 -0700 Subject: [PATCH] Use RelaxedPropertyResolver in auto-configuration Update several existing auto-configuration classes to use the new RelaxedPropertyResolver. This commit also rename the spring.template property to spring.thymeleaf in case we wish to support more templating engines in the future. --- .../MessageSourceAutoConfiguration.java | 17 +++++++--- .../batch/BatchDatabaseInitializer.java | 22 +++++++++---- .../thymeleaf/ThymeleafAutoConfiguration.java | 33 ++++++++++--------- .../batch/BatchAutoConfigurationTests.java | 6 ++-- .../ThymeleafAutoConfigurationTests.java | 8 ++--- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java index a2920ae973..17a7270629 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java @@ -16,14 +16,16 @@ package org.springframework.boot.autoconfigure; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.context.EnvironmentAware; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; /** * {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}. @@ -33,15 +35,20 @@ import org.springframework.core.annotation.Order; @Configuration @ConditionalOnMissingBean(MessageSource.class) @Order(Ordered.HIGHEST_PRECEDENCE) -public class MessageSourceAutoConfiguration { +public class MessageSourceAutoConfiguration implements EnvironmentAware { - @Value("${spring.messages.basename:messages}") - private String basename; + private RelaxedPropertyResolver environment; + + @Override + public void setEnvironment(Environment environment) { + this.environment = new RelaxedPropertyResolver(environment, "spring.messages."); + } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - messageSource.setBasename(this.basename); + String basename = this.environment.getProperty("basename", "messages"); + messageSource.setBasename(basename); return messageSource; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java index 3b8514abd8..a6483cc229 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java @@ -21,7 +21,9 @@ import javax.sql.DataSource; import org.springframework.batch.support.DatabaseType; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; @@ -33,7 +35,9 @@ import org.springframework.stereotype.Component; * @author Dave Syer */ @Component -public class BatchDatabaseInitializer { +public class BatchDatabaseInitializer implements EnvironmentAware { + + private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/batch/core/schema-@@platform@@.sql"; @Autowired private DataSource dataSource; @@ -41,8 +45,12 @@ public class BatchDatabaseInitializer { @Autowired private ResourceLoader resourceLoader; - @Value("${spring.batch.schema:classpath:org/springframework/batch/core/schema-@@platform@@.sql}") - private String schemaLocation = "classpath:org/springframework/batch/core/schema-@@platform@@.sql"; + private RelaxedPropertyResolver environment; + + @Override + public void setEnvironment(Environment environment) { + this.environment = new RelaxedPropertyResolver(environment, "spring.batch."); + } @PostConstruct protected void initialize() throws Exception { @@ -52,8 +60,10 @@ public class BatchDatabaseInitializer { platform = "hsqldb"; } ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); - populator.addScript(this.resourceLoader.getResource(this.schemaLocation.replace( - "@@platform@@", platform))); + String schemaLocation = this.environment.getProperty("schema", + DEFAULT_SCHEMA_LOCATION); + schemaLocation = schemaLocation.replace("@@platform@@", platform); + populator.addScript(this.resourceLoader.getResource(schemaLocation)); populator.setContinueOnError(true); DatabasePopulatorUtils.execute(populator, this.dataSource); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index ebcbbc797c..43f1133504 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -26,15 +26,17 @@ import javax.servlet.Servlet; import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.thymeleaf.TemplateProcessingParameters; @@ -58,22 +60,19 @@ public class ThymeleafAutoConfiguration { @Configuration @ConditionalOnMissingBean(name = "defaultTemplateResolver") - protected static class DefaultTemplateResolverConfiguration { + protected static class DefaultTemplateResolverConfiguration implements + EnvironmentAware { @Autowired private ResourceLoader resourceLoader = new DefaultResourceLoader(); - @Value("${spring.template.prefix:classpath:/templates/}") - private String prefix = "classpath:/templates/"; + private RelaxedPropertyResolver environment; - @Value("${spring.template.suffix:.html}") - private String suffix = ".html"; - - @Value("${spring.template.cache:true}") - private boolean cacheable; - - @Value("${spring.template.mode:HTML5}") - private String templateMode = "HTML5"; + @Override + public void setEnvironment(Environment environment) { + this.environment = new RelaxedPropertyResolver(environment, + "spring.thymeleaf."); + } @Bean public ITemplateResolver defaultTemplateResolver() { @@ -97,10 +96,12 @@ public class ThymeleafAutoConfiguration { return "SPRING"; } }); - resolver.setPrefix(this.prefix); - resolver.setSuffix(this.suffix); - resolver.setTemplateMode(this.templateMode); - resolver.setCacheable(this.cacheable); + resolver.setPrefix(this.environment.getProperty("prefix", + "classpath:/templates/")); + resolver.setSuffix(this.environment.getProperty("suffix", ".html")); + resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5")); + resolver.setCacheable(this.environment.getProperty("cache", Boolean.class, + true)); return resolver; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 7be7b84c29..92e289202d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -34,7 +34,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; import org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner; -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConfiguration; +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -53,7 +53,7 @@ public class BatchAutoConfigurationTests { public void testDefaultContext() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, BatchAutoConfiguration.class, - EmbeddedDatabaseConfiguration.class, + EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); @@ -63,7 +63,7 @@ public class BatchAutoConfigurationTests { public void testDefinesAndLaunchesJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(JobConfiguration.class, BatchAutoConfiguration.class, - EmbeddedDatabaseConfiguration.class, + EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index 980beaf98a..58d832e7ac 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -23,7 +23,6 @@ import java.util.Map; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.env.MapPropertySource; import org.springframework.mock.web.MockHttpServletRequest; @@ -40,7 +39,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** - * Tests for {@link ThymeleafAutoConfiguration} + * Tests for {@link ThymeleafAutoConfiguration}. + * * @author Dave Syer */ public class ThymeleafAutoConfigurationTests { @@ -51,8 +51,8 @@ public class ThymeleafAutoConfigurationTests { context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Map map = new HashMap(); - map.put("spring.template.mode", "XHTML"); - map.put("spring.template.suffix", ""); + map.put("spring.thymeleaf.mode", "XHTML"); + map.put("spring.thymeleaf.suffix", ""); context.getEnvironment().getPropertySources() .addFirst(new MapPropertySource("test", map)); context.refresh();