From dac03fdb7b57d83699ce43c1c2a484616b2e8623 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 9 May 2014 23:43:00 +0100 Subject: [PATCH] Extract WebMvcProperties Extract WebMvcProperties from WebMvcAutoConfiguration and also update conditionals to use @ConditionalOnProperty. --- .../web/WebMvcAutoConfiguration.java | 28 ++++----- .../autoconfigure/web/WebMvcProperties.java | 62 +++++++++++++++++++ .../web/WebMvcAutoConfigurationTests.java | 4 +- .../appendix-application-properties.adoc | 2 +- 4 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index f4fd6db2b9..1f6a823571 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -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 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; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java new file mode 100644 index 0000000000..632768a3d3 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java @@ -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; + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index d55c705cb0..af9ebee085 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -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, diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index c21d81c987..bbc3660a17 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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