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:
parent
0a7ac89984
commit
2041a79970
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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<String, Object> map = new HashMap<String, Object>();
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue