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.
This commit is contained in:
Phillip Webb 2013-08-22 01:47:08 -07:00
parent 0a7ac89984
commit 2041a79970
5 changed files with 52 additions and 34 deletions

View File

@ -16,14 +16,16 @@
package org.springframework.boot.autoconfigure; package org.springframework.boot.autoconfigure;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.MessageSource;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}. * {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
@ -33,15 +35,20 @@ import org.springframework.core.annotation.Order;
@Configuration @Configuration
@ConditionalOnMissingBean(MessageSource.class) @ConditionalOnMissingBean(MessageSource.class)
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
public class MessageSourceAutoConfiguration { public class MessageSourceAutoConfiguration implements EnvironmentAware {
@Value("${spring.messages.basename:messages}") private RelaxedPropertyResolver environment;
private String basename;
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment, "spring.messages.");
}
@Bean @Bean
public MessageSource messageSource() { public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(this.basename); String basename = this.environment.getProperty("basename", "messages");
messageSource.setBasename(basename);
return messageSource; return messageSource;
} }

View File

@ -21,7 +21,9 @@ import javax.sql.DataSource;
import org.springframework.batch.support.DatabaseType; import org.springframework.batch.support.DatabaseType;
import org.springframework.beans.factory.annotation.Autowired; 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.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@ -33,7 +35,9 @@ import org.springframework.stereotype.Component;
* @author Dave Syer * @author Dave Syer
*/ */
@Component @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 @Autowired
private DataSource dataSource; private DataSource dataSource;
@ -41,8 +45,12 @@ public class BatchDatabaseInitializer {
@Autowired @Autowired
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
@Value("${spring.batch.schema:classpath:org/springframework/batch/core/schema-@@platform@@.sql}") private RelaxedPropertyResolver environment;
private String schemaLocation = "classpath:org/springframework/batch/core/schema-@@platform@@.sql";
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment, "spring.batch.");
}
@PostConstruct @PostConstruct
protected void initialize() throws Exception { protected void initialize() throws Exception {
@ -52,8 +60,10 @@ public class BatchDatabaseInitializer {
platform = "hsqldb"; platform = "hsqldb";
} }
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(this.resourceLoader.getResource(this.schemaLocation.replace( String schemaLocation = this.environment.getProperty("schema",
"@@platform@@", platform))); DEFAULT_SCHEMA_LOCATION);
schemaLocation = schemaLocation.replace("@@platform@@", platform);
populator.addScript(this.resourceLoader.getResource(schemaLocation));
populator.setContinueOnError(true); populator.setContinueOnError(true);
DatabasePopulatorUtils.execute(populator, this.dataSource); DatabasePopulatorUtils.execute(populator, this.dataSource);
} }

View File

@ -26,15 +26,17 @@ import javax.servlet.Servlet;
import nz.net.ultraq.thymeleaf.LayoutDialect; import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.beans.factory.annotation.Autowired; 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.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.TemplateProcessingParameters; import org.thymeleaf.TemplateProcessingParameters;
@ -58,22 +60,19 @@ public class ThymeleafAutoConfiguration {
@Configuration @Configuration
@ConditionalOnMissingBean(name = "defaultTemplateResolver") @ConditionalOnMissingBean(name = "defaultTemplateResolver")
protected static class DefaultTemplateResolverConfiguration { protected static class DefaultTemplateResolverConfiguration implements
EnvironmentAware {
@Autowired @Autowired
private ResourceLoader resourceLoader = new DefaultResourceLoader(); private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Value("${spring.template.prefix:classpath:/templates/}") private RelaxedPropertyResolver environment;
private String prefix = "classpath:/templates/";
@Value("${spring.template.suffix:.html}") @Override
private String suffix = ".html"; public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
@Value("${spring.template.cache:true}") "spring.thymeleaf.");
private boolean cacheable; }
@Value("${spring.template.mode:HTML5}")
private String templateMode = "HTML5";
@Bean @Bean
public ITemplateResolver defaultTemplateResolver() { public ITemplateResolver defaultTemplateResolver() {
@ -97,10 +96,12 @@ public class ThymeleafAutoConfiguration {
return "SPRING"; return "SPRING";
} }
}); });
resolver.setPrefix(this.prefix); resolver.setPrefix(this.environment.getProperty("prefix",
resolver.setSuffix(this.suffix); "classpath:/templates/"));
resolver.setTemplateMode(this.templateMode); resolver.setSuffix(this.environment.getProperty("suffix", ".html"));
resolver.setCacheable(this.cacheable); resolver.setTemplateMode(this.environment.getProperty("mode", "HTML5"));
resolver.setCacheable(this.environment.getProperty("cache", Boolean.class,
true));
return resolver; return resolver;
} }

View File

@ -34,7 +34,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
import org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner; 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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -53,7 +53,7 @@ public class BatchAutoConfigurationTests {
public void testDefaultContext() throws Exception { public void testDefaultContext() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, BatchAutoConfiguration.class, this.context.register(TestConfiguration.class, BatchAutoConfiguration.class,
EmbeddedDatabaseConfiguration.class, EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(JobLauncher.class)); assertNotNull(this.context.getBean(JobLauncher.class));
@ -63,7 +63,7 @@ public class BatchAutoConfigurationTests {
public void testDefinesAndLaunchesJob() throws Exception { public void testDefinesAndLaunchesJob() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(JobConfiguration.class, BatchAutoConfiguration.class, this.context.register(JobConfiguration.class, BatchAutoConfiguration.class,
EmbeddedDatabaseConfiguration.class, EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(JobLauncher.class)); assertNotNull(this.context.getBean(JobLauncher.class));

View File

@ -23,7 +23,6 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
@ -40,7 +39,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
* Tests for {@link ThymeleafAutoConfiguration} * Tests for {@link ThymeleafAutoConfiguration}.
*
* @author Dave Syer * @author Dave Syer
*/ */
public class ThymeleafAutoConfigurationTests { public class ThymeleafAutoConfigurationTests {
@ -51,8 +51,8 @@ public class ThymeleafAutoConfigurationTests {
context.register(ThymeleafAutoConfiguration.class, context.register(ThymeleafAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("spring.template.mode", "XHTML"); map.put("spring.thymeleaf.mode", "XHTML");
map.put("spring.template.suffix", ""); map.put("spring.thymeleaf.suffix", "");
context.getEnvironment().getPropertySources() context.getEnvironment().getPropertySources()
.addFirst(new MapPropertySource("test", map)); .addFirst(new MapPropertySource("test", map));
context.refresh(); context.refresh();