Skip to content

Commit 8ce4110

Browse files
committed
Allow Jersey-based Actuator to be used with separate management port
Previously, when the Jersey-based Actuator was configure with a separate management port, the resulting child context would fail to start due to their being no ResourceConfigCustomizer beans available. This commit updates the configuration so that the customizer's are injected using an ObjectProvider and an empty list is used in the event of their being no customizer beans. This aligns the child context configuration class with JerseyAutoConfiguration which already used this approach. Closes gh-12975
1 parent e45384b commit 8ce4110

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/jersey/JerseyManagementChildContextConfiguration.java

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

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

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

2122
import org.glassfish.jersey.server.ResourceConfig;
2223
import org.glassfish.jersey.servlet.ServletContainer;
2324

25+
import org.springframework.beans.factory.ObjectProvider;
2426
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2527
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
2628
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -49,8 +51,9 @@ public class JerseyManagementChildContextConfiguration {
4951
private final List<ResourceConfigCustomizer> resourceConfigCustomizers;
5052

5153
public JerseyManagementChildContextConfiguration(
52-
List<ResourceConfigCustomizer> resourceConfigCustomizers) {
53-
this.resourceConfigCustomizers = resourceConfigCustomizers;
54+
ObjectProvider<List<ResourceConfigCustomizer>> resourceConfigCustomizers) {
55+
this.resourceConfigCustomizers = resourceConfigCustomizers
56+
.getIfAvailable(Collections::emptyList);
5457
}
5558

5659
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.jersey;
18+
19+
import org.glassfish.jersey.server.ResourceConfig;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
24+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
25+
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
26+
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
33+
34+
/**
35+
* Tests for {@link JerseyManagementChildContextConfiguration}.
36+
*
37+
* @author Andy Wilkinson
38+
*/
39+
@RunWith(ModifiedClassPathRunner.class)
40+
@ClassPathExclusions("spring-webmvc-*")
41+
public class JerseyManagementChildContextConfigurationTests {
42+
43+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
44+
.withUserConfiguration(JerseyManagementChildContextConfiguration.class);
45+
46+
@Test
47+
public void resourceConfigIsCustomizedWithResourceConfigCustomizerBean() {
48+
this.contextRunner.withUserConfiguration(CustomizerConfiguration.class)
49+
.run((context) -> {
50+
assertThat(context).hasSingleBean(ResourceConfig.class);
51+
ResourceConfig config = context.getBean(ResourceConfig.class);
52+
ResourceConfigCustomizer customizer = context
53+
.getBean(ResourceConfigCustomizer.class);
54+
verify(customizer).customize(config);
55+
});
56+
}
57+
58+
@Test
59+
public void resourceConfigCustomizerBeanIsNotRequired() {
60+
this.contextRunner.run(
61+
(context) -> assertThat(context).hasSingleBean(ResourceConfig.class));
62+
}
63+
64+
@Configuration
65+
static class CustomizerConfiguration {
66+
67+
@Bean
68+
ResourceConfigCustomizer resourceConfigCustomizer() {
69+
return mock(ResourceConfigCustomizer.class);
70+
}
71+
72+
}
73+
74+
}

0 commit comments

Comments
 (0)