From fdccd8aaa79be143943a5cb5a412d19605fb03ad Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 1 Dec 2016 11:51:29 +0000 Subject: [PATCH] Remove use of field injection from ErrorMvcAutoConfiguration --- .../web/ErrorMvcAutoConfiguration.java | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java index f6f4374a3b..4ca95579be 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java @@ -27,7 +27,7 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.aop.framework.autoproxy.AutoProxyUtils; import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureBefore; @@ -82,20 +82,14 @@ import org.springframework.web.util.HtmlUtils; @EnableConfigurationProperties(ResourceProperties.class) public class ErrorMvcAutoConfiguration { - private final ApplicationContext applicationContext; - private final ServerProperties serverProperties; - private final ResourceProperties resourceProperties; - - @Autowired(required = false) - private List errorViewResolvers; + private final List errorViewResolvers; - public ErrorMvcAutoConfiguration(ApplicationContext applicationContext, - ServerProperties serverProperties, ResourceProperties resourceProperties) { - this.applicationContext = applicationContext; + public ErrorMvcAutoConfiguration(ServerProperties serverProperties, + ObjectProvider> errorViewResolversProvider) { this.serverProperties = serverProperties; - this.resourceProperties = resourceProperties; + this.errorViewResolvers = errorViewResolversProvider.getIfAvailable(); } @Bean @@ -116,19 +110,34 @@ public class ErrorMvcAutoConfiguration { return new ErrorPageCustomizer(this.serverProperties); } - @Bean - @ConditionalOnBean(DispatcherServlet.class) - @ConditionalOnMissingBean - public DefaultErrorViewResolver conventionErrorViewResolver() { - return new DefaultErrorViewResolver(this.applicationContext, - this.resourceProperties); - } - @Bean public static PreserveErrorControllerTargetClassPostProcessor preserveErrorControllerTargetClassPostProcessor() { return new PreserveErrorControllerTargetClassPostProcessor(); } + @Configuration + static class DefaultErrorViewResolverConfiguration { + + private final ApplicationContext applicationContext; + + private final ResourceProperties resourceProperties; + + DefaultErrorViewResolverConfiguration(ApplicationContext applicationContext, + ResourceProperties resourceProperties) { + this.applicationContext = applicationContext; + this.resourceProperties = resourceProperties; + } + + @Bean + @ConditionalOnBean(DispatcherServlet.class) + @ConditionalOnMissingBean + public DefaultErrorViewResolver conventionErrorViewResolver() { + return new DefaultErrorViewResolver(this.applicationContext, + this.resourceProperties); + } + + } + @Configuration @ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true) @Conditional(ErrorTemplateMissingCondition.class)