Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.context.annotation.AnnotationConfigRegistry;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;

/**
Expand All @@ -40,6 +41,7 @@
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author Yongjun Hong
* @since 4.0.0
*/
public final class ManagementContextFactory {
Expand All @@ -63,7 +65,11 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
.createEnvironment(this.webApplicationType);
Assert.state(childEnvironment != null, "'childEnvironment' must not be null");
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
childEnvironment.setConversionService((configurableEnvironment).getConversionService());
configurableEnvironment.getPropertySources().forEach((propertySource) -> {
if (isManagementPropertySource(propertySource, childEnvironment)) {
childEnvironment.getPropertySources().addLast(propertySource);
}
});
}
ConfigurableApplicationContext managementContext = ApplicationContextFactory.DEFAULT
.create(this.webApplicationType);
Expand All @@ -73,6 +79,12 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
return managementContext;
}

private boolean isManagementPropertySource(PropertySource<?> propertySource,
ConfigurableEnvironment childEnvironment) {
return propertySource.getName().contains("management")
&& !childEnvironment.getPropertySources().contains(propertySource.getName());
}

public void registerWebServerFactoryBeans(ApplicationContext parentContext,
ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) {
if (this.autoConfigurationClasses != null && this.autoConfigurationClasses.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.web.server;

import org.junit.jupiter.api.Test;

import org.springframework.boot.WebApplicationType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link ManagementContextFactory}.
*
* author Yongjun Hong
*/
class ManagementContextFactoryTests {

@Test
void createManagementContextCopiesManagementPropertySources() {
ApplicationContext parentContext = mock(ApplicationContext.class);
ConfigurableEnvironment parentEnvironment = mock(ConfigurableEnvironment.class);
MutablePropertySources parentPropertySources = new MutablePropertySources();
PropertySource<?> managementPropertySource = new PropertySource<>("managementProperty") {
@Override
public Object getProperty(String name) {
return null;
}
};
parentPropertySources.addLast(managementPropertySource);
given(parentEnvironment.getPropertySources()).willReturn(parentPropertySources);
given(parentContext.getEnvironment()).willReturn(parentEnvironment);

ManagementContextFactory factory = new ManagementContextFactory(WebApplicationType.SERVLET, null);

ConfigurableApplicationContext managementContext = factory.createManagementContext(parentContext);

ConfigurableEnvironment childEnvironment = managementContext.getEnvironment();
assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue();
}

}
Loading