From 967625db1e66930169316388a626a50f807cdba7 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 5 Dec 2016 10:48:01 +0000 Subject: [PATCH] Replace field injection with constructor injection in config classes Closes gh-7563 --- ...tWebMvcManagementContextConfiguration.java | 36 ++++++++++++------- .../IntegrationAutoConfiguration.java | 11 +++--- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java index d58fdc0cb7..356a10c5d8 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java @@ -16,10 +16,11 @@ package org.springframework.boot.actuate.autoconfigure; +import java.util.Collections; import java.util.List; import java.util.Set; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; @@ -67,17 +68,28 @@ import org.springframework.web.cors.CorsConfiguration; EndpointCorsProperties.class }) public class EndpointWebMvcManagementContextConfiguration { - @Autowired - private HealthMvcEndpointProperties healthMvcEndpointProperties; + private final HealthMvcEndpointProperties healthMvcEndpointProperties; - @Autowired - private ManagementServerProperties managementServerProperties; + private final ManagementServerProperties managementServerProperties; - @Autowired - private EndpointCorsProperties corsProperties; + private final EndpointCorsProperties corsProperties; - @Autowired(required = false) - private List mappingCustomizers; + private final List mappingCustomizers; + + public EndpointWebMvcManagementContextConfiguration( + HealthMvcEndpointProperties healthMvcEndpointProperties, + ManagementServerProperties managementServerProperties, + EndpointCorsProperties corsProperties, + ObjectProvider> mappingCustomizersProvider) { + this.healthMvcEndpointProperties = healthMvcEndpointProperties; + this.managementServerProperties = managementServerProperties; + this.corsProperties = corsProperties; + List providedCustomizers = mappingCustomizersProvider + .getIfAvailable(); + this.mappingCustomizers = providedCustomizers == null + ? Collections.emptyList() + : providedCustomizers; + } @Bean @ConditionalOnMissingBean @@ -87,10 +99,8 @@ public class EndpointWebMvcManagementContextConfiguration { EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints, corsConfiguration); mapping.setPrefix(this.managementServerProperties.getContextPath()); - if (this.mappingCustomizers != null) { - for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) { - customizer.customize(mapping); - } + for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) { + customizer.customize(mapping); } return mapping; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java index 7bee106b3b..c5b0015c06 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java @@ -21,7 +21,7 @@ import javax.management.MBeanServer; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -66,13 +66,16 @@ public class IntegrationAutoConfiguration { protected static class IntegrationJmxConfiguration implements EnvironmentAware, BeanFactoryAware { + private final IntegrationManagementConfigurer configurer; + private BeanFactory beanFactory; private RelaxedPropertyResolver propertyResolver; - @Autowired(required = false) - @Qualifier(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME) - private IntegrationManagementConfigurer configurer; + protected IntegrationJmxConfiguration( + @Qualifier(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME) ObjectProvider configurerProvider) { + this.configurer = configurerProvider.getIfAvailable(); + } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException {