Extract WebMvcProperties

Extract WebMvcProperties from WebMvcAutoConfiguration and also
update conditionals to use @ConditionalOnProperty.
pull/837/head
Phillip Webb 11 years ago
parent 1c0cce441d
commit dac03fdb7b

@ -35,8 +35,8 @@ 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.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -128,7 +128,7 @@ public class WebMvcAutoConfiguration {
// on the classpath
@Configuration
@EnableWebMvc
@EnableConfigurationProperties(ResourceProperties.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);
@ -142,14 +142,8 @@ public class WebMvcAutoConfiguration {
@Autowired
private ResourceProperties resourceProperties = new ResourceProperties();
@Value("${spring.mvc.message-codes-resolver.format:}")
private DefaultMessageCodesResolver.Format messageCodesResolverFormat = null;
@Value("${spring.mvc.locale:}")
private String locale = "";
@Value("${spring.mvc.date-format:}")
private String dateFormat = "";
@Autowired
private WebMvcProperties mvcProperties = new WebMvcProperties();
@Autowired
private ListableBeanFactory beanFactory;
@ -203,22 +197,24 @@ public class WebMvcAutoConfiguration {
@Bean
@ConditionalOnMissingBean(LocaleResolver.class)
@ConditionalOnExpression("'${spring.mvc.locale:}' != ''")
@ConditionalOnProperty(prefix = "spring.mvc.", value = "locale")
public LocaleResolver localeResolver() {
return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale));
return new FixedLocaleResolver(
StringUtils.parseLocaleString(this.mvcProperties.getLocale()));
}
@Bean
@ConditionalOnExpression("'${spring.mvc.date-format:}' != ''")
@ConditionalOnProperty(prefix = "spring.mvc.", value = "date-format")
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.dateFormat);
return new DateFormatter(this.mvcProperties.getDateFormat());
}
@Override
public MessageCodesResolver getMessageCodesResolver() {
if (this.messageCodesResolverFormat != null) {
if (this.mvcProperties.getMessageCodesResolverFormat() != null) {
DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver();
resolver.setMessageCodeFormatter(this.messageCodesResolverFormat);
resolver.setMessageCodeFormatter(this.mvcProperties
.getMessageCodesResolverFormat());
return resolver;
}
return null;

@ -0,0 +1,62 @@
/*
* 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.web;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.DefaultMessageCodesResolver;
/**
* {@link ConfigurationProperties properties} for Spring MVC.
*
* @author Phillip Webb
* @since 1.1
*/
@ConfigurationProperties("spring.mvc")
public class WebMvcProperties {
private DefaultMessageCodesResolver.Format messageCodesResolverFormat;
private String locale;
private String dateFormat;
public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
return this.messageCodesResolverFormat;
}
public void setMessageCodesResolverFormat(
DefaultMessageCodesResolver.Format messageCodesResolverFormat) {
this.messageCodesResolverFormat = messageCodesResolverFormat;
}
public String getLocale() {
return this.locale;
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getDateFormat() {
return this.dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
}

@ -220,7 +220,7 @@ public class WebMvcAutoConfigurationTests {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
// set fixed date format
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.date-format:dd*MM*yyyy");
"spring.mvc.dateFormat:dd*MM*yyyy");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
@ -248,7 +248,7 @@ public class WebMvcAutoConfigurationTests {
public void overrideMessageCodesFormat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.message-codes-resolver.format:POSTFIX_ERROR_CODE");
"spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE");
this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,

@ -69,7 +69,7 @@ content into your application; rather pick only the properties that you need.
http.mappers.json-sort-keys=false # sort keys
spring.mvc.locale= # set fixed locale, e.g. en_UK
spring.mvc.date-format= # set fixed date format, e.g. dd/MM/yyyy
spring.mvc.message-codes-resolver.format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix
spring.resources.cache-period= # cache timeouts in headers sent to browser

Loading…
Cancel
Save