Skip to content

Commit 967625d

Browse files
committed
Replace field injection with constructor injection in config classes
Closes spring-projectsgh-7563
1 parent 5ac75c9 commit 967625d

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure;
1818

19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Set;
2122

22-
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.ObjectProvider;
2324
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
2425
import org.springframework.boot.actuate.endpoint.Endpoint;
2526
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
@@ -67,17 +68,28 @@
6768
EndpointCorsProperties.class })
6869
public class EndpointWebMvcManagementContextConfiguration {
6970

70-
@Autowired
71-
private HealthMvcEndpointProperties healthMvcEndpointProperties;
71+
private final HealthMvcEndpointProperties healthMvcEndpointProperties;
7272

73-
@Autowired
74-
private ManagementServerProperties managementServerProperties;
73+
private final ManagementServerProperties managementServerProperties;
7574

76-
@Autowired
77-
private EndpointCorsProperties corsProperties;
75+
private final EndpointCorsProperties corsProperties;
7876

79-
@Autowired(required = false)
80-
private List<EndpointHandlerMappingCustomizer> mappingCustomizers;
77+
private final List<EndpointHandlerMappingCustomizer> mappingCustomizers;
78+
79+
public EndpointWebMvcManagementContextConfiguration(
80+
HealthMvcEndpointProperties healthMvcEndpointProperties,
81+
ManagementServerProperties managementServerProperties,
82+
EndpointCorsProperties corsProperties,
83+
ObjectProvider<List<EndpointHandlerMappingCustomizer>> mappingCustomizersProvider) {
84+
this.healthMvcEndpointProperties = healthMvcEndpointProperties;
85+
this.managementServerProperties = managementServerProperties;
86+
this.corsProperties = corsProperties;
87+
List<EndpointHandlerMappingCustomizer> providedCustomizers = mappingCustomizersProvider
88+
.getIfAvailable();
89+
this.mappingCustomizers = providedCustomizers == null
90+
? Collections.<EndpointHandlerMappingCustomizer>emptyList()
91+
: providedCustomizers;
92+
}
8193

8294
@Bean
8395
@ConditionalOnMissingBean
@@ -87,10 +99,8 @@ public EndpointHandlerMapping endpointHandlerMapping() {
8799
EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints,
88100
corsConfiguration);
89101
mapping.setPrefix(this.managementServerProperties.getContextPath());
90-
if (this.mappingCustomizers != null) {
91-
for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) {
92-
customizer.customize(mapping);
93-
}
102+
for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) {
103+
customizer.customize(mapping);
94104
}
95105
return mapping;
96106
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.beans.BeansException;
2222
import org.springframework.beans.factory.BeanFactory;
2323
import org.springframework.beans.factory.BeanFactoryAware;
24-
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.beans.factory.ObjectProvider;
2525
import org.springframework.beans.factory.annotation.Qualifier;
2626
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -66,13 +66,16 @@ protected static class IntegrationConfiguration {
6666
protected static class IntegrationJmxConfiguration
6767
implements EnvironmentAware, BeanFactoryAware {
6868

69+
private final IntegrationManagementConfigurer configurer;
70+
6971
private BeanFactory beanFactory;
7072

7173
private RelaxedPropertyResolver propertyResolver;
7274

73-
@Autowired(required = false)
74-
@Qualifier(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)
75-
private IntegrationManagementConfigurer configurer;
75+
protected IntegrationJmxConfiguration(
76+
@Qualifier(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME) ObjectProvider<IntegrationManagementConfigurer> configurerProvider) {
77+
this.configurer = configurerProvider.getIfAvailable();
78+
}
7679

7780
@Override
7881
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

0 commit comments

Comments
 (0)