Switch from @Bean to protected method

This commit is contained in:
Dave Syer 2014-05-01 10:28:35 +01:00
parent 524e95eeb2
commit ed64640ea4
2 changed files with 23 additions and 18 deletions

View File

@ -200,13 +200,14 @@ public class WebMvcAutoConfiguration {
return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale)); return new FixedLocaleResolver(StringUtils.parseLocaleString(this.locale));
} }
@Bean @Override
@ConditionalOnMissingBean(MessageCodesResolver.class) public MessageCodesResolver getMessageCodesResolver() {
@ConditionalOnExpression("'${spring.mvc.message-codes-resolver.format:}' != ''") if (this.messageCodesResolverFormat != null) {
public MessageCodesResolver messageCodesResolver() { DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver();
DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver(); resolver.setMessageCodeFormatter(this.messageCodesResolverFormat);
resolver.setMessageCodeFormatter(this.messageCodesResolverFormat); return resolver;
return resolver; }
return null;
} }
@Override @Override

View File

@ -16,12 +16,6 @@
package org.springframework.boot.autoconfigure.web; package org.springframework.boot.autoconfigure.web;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -37,6 +31,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
@ -49,7 +44,6 @@ import org.springframework.core.io.Resource;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
@ -62,6 +56,14 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link WebMvcAutoConfiguration}. * Tests for {@link WebMvcAutoConfiguration}.
* *
@ -181,15 +183,16 @@ public class WebMvcAutoConfigurationTests {
assertThat(locale.toString(), equalTo("en_UK")); assertThat(locale.toString(), equalTo("en_UK"));
} }
@Test(expected = NoSuchBeanDefinitionException.class) @Test
public void noMessageCodeResolver() throws Exception { public void noMessageCodesResolver() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(AllResources.class, Config.class, this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
this.context.getBean(MessageCodesResolver.class); assertNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
.getMessageCodesResolver());
} }
@Test @Test
@ -202,7 +205,8 @@ public class WebMvcAutoConfigurationTests {
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
this.context.getBean(MessageCodesResolver.class); assertNotNull(this.context.getBean(WebMvcAutoConfigurationAdapter.class)
.getMessageCodesResolver());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")