Disable JMX by default

This commit switches the default value for the `spring.jmx.enabled`
configuration property.
JMX is now disabled by default and can be enabled with
`spring.jmx.enabled=true`.

Closes gh-16090
This commit is contained in:
Brian Clozel 2019-03-05 15:30:19 +01:00
parent d403dae6fc
commit ce9626d00f
8 changed files with 36 additions and 34 deletions

View File

@ -50,7 +50,7 @@ public class JmxEndpointIntegrationTests {
EndpointAutoConfiguration.class, JmxEndpointAutoConfiguration.class, EndpointAutoConfiguration.class, JmxEndpointAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class, HealthIndicatorAutoConfiguration.class,
HttpTraceAutoConfiguration.class)) HttpTraceAutoConfiguration.class))
.withConfiguration( .withPropertyValues("spring.jmx.enabled=true").withConfiguration(
AutoConfigurations.of(EndpointAutoConfigurationClasses.ALL)); AutoConfigurations.of(EndpointAutoConfigurationClasses.ALL));
@Test @Test

View File

@ -36,7 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
public class KafkaMetricsAutoConfigurationTests { public class KafkaMetricsAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple()).withConfiguration( .with(MetricsRun.simple()).withPropertyValues("spring.jmx.enabled=true")
.withConfiguration(
AutoConfigurations.of(KafkaMetricsAutoConfiguration.class)); AutoConfigurations.of(KafkaMetricsAutoConfiguration.class));
@Test @Test

View File

@ -42,7 +42,7 @@ import org.springframework.util.StringUtils;
* {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's * {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's
* {@link EnableMBeanExport} mechanism based on configuration properties. * {@link EnableMBeanExport} mechanism based on configuration properties.
* <p> * <p>
* To disable auto export of annotation beans set {@code spring.jmx.enabled: false}. * To enable auto export of annotation beans set {@code spring.jmx.enabled: true}.
* *
* @author Christian Dupuis * @author Christian Dupuis
* @author Madhura Bhave * @author Madhura Bhave
@ -50,7 +50,7 @@ import org.springframework.util.StringUtils;
*/ */
@Configuration @Configuration
@ConditionalOnClass({ MBeanExporter.class }) @ConditionalOnClass({ MBeanExporter.class })
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true")
public class JmxAutoConfiguration { public class JmxAutoConfiguration {
private final Environment environment; private final Environment environment;

View File

@ -463,7 +463,7 @@
"name": "spring.jmx.enabled", "name": "spring.jmx.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"description": "Expose management beans to the JMX domain.", "description": "Expose management beans to the JMX domain.",
"defaultValue": true "defaultValue": false
}, },
{ {
"name": "spring.jmx.server", "name": "spring.jmx.server",

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -101,31 +101,30 @@ public class IntegrationAutoConfigurationTests {
} }
@Test @Test
public void jmxIntegrationEnabledByDefault() { public void enableJmxIntegration() {
this.contextRunner.run((context) -> { this.contextRunner.withPropertyValues("spring.jmx.enabled=true")
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
assertThat(mBeanServer.getDomains()).contains(
"org.springframework.integration",
"org.springframework.integration.monitor");
assertThat(context)
.hasBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
});
}
@Test
public void disableJmxIntegration() {
this.contextRunner.withPropertyValues("spring.jmx.enabled=false")
.run((context) -> { .run((context) -> {
assertThat(context).doesNotHaveBean(MBeanServer.class); MBeanServer mBeanServer = context.getBean(MBeanServer.class);
assertThat(context) assertThat(mBeanServer.getDomains()).contains(
.hasSingleBean(IntegrationManagementConfigurer.class); "org.springframework.integration",
"org.springframework.integration.monitor");
assertThat(context).hasBean(
IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
}); });
} }
@Test
public void jmxIntegrationIsDisabledByDefault() {
this.contextRunner.run((context) -> {
assertThat(context).doesNotHaveBean(MBeanServer.class);
assertThat(context).hasSingleBean(IntegrationManagementConfigurer.class);
});
}
@Test @Test
public void customizeJmxDomain() { public void customizeJmxDomain() {
this.contextRunner.withPropertyValues("spring.jmx.default_domain=org.foo") this.contextRunner.withPropertyValues("spring.jmx.enabled=true",
.run((context) -> { "spring.jmx.default_domain=org.foo").run((context) -> {
MBeanServer mBeanServer = context.getBean(MBeanServer.class); MBeanServer mBeanServer = context.getBean(MBeanServer.class);
assertThat(mBeanServer.getDomains()).contains("org.foo") assertThat(mBeanServer.getDomains()).contains("org.foo")
.doesNotContain("org.springframework.integration", .doesNotContain("org.springframework.integration",
@ -135,8 +134,8 @@ public class IntegrationAutoConfigurationTests {
@Test @Test
public void primaryExporterIsAllowed() { public void primaryExporterIsAllowed() {
this.contextRunner.withUserConfiguration(CustomMBeanExporter.class) this.contextRunner.withPropertyValues("spring.jmx.enabled=true")
.run((context) -> { .withUserConfiguration(CustomMBeanExporter.class).run((context) -> {
assertThat(context).getBeans(MBeanExporter.class).hasSize(2); assertThat(context).getBeans(MBeanExporter.class).hasSize(2);
assertThat(context.getBean(MBeanExporter.class)) assertThat(context.getBean(MBeanExporter.class))
.isSameAs(context.getBean("myMBeanExporter")); .isSameAs(context.getBean("myMBeanExporter"));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,7 +60,7 @@ public class DataSourceJmxConfigurationTests {
public void hikariAutoConfiguredCanUseRegisterMBeans() { public void hikariAutoConfiguredCanUseRegisterMBeans() {
String poolName = UUID.randomUUID().toString(); String poolName = UUID.randomUUID().toString();
this.contextRunner this.contextRunner
.withPropertyValues( .withPropertyValues("spring.jmx.enabled=true",
"spring.datasource.type=" + HikariDataSource.class.getName(), "spring.datasource.type=" + HikariDataSource.class.getName(),
"spring.datasource.name=" + poolName, "spring.datasource.name=" + poolName,
"spring.datasource.hikari.register-mbeans=true") "spring.datasource.hikari.register-mbeans=true")
@ -118,7 +118,7 @@ public class DataSourceJmxConfigurationTests {
public void hikariProxiedCanUseRegisterMBeans() { public void hikariProxiedCanUseRegisterMBeans() {
String poolName = UUID.randomUUID().toString(); String poolName = UUID.randomUUID().toString();
this.contextRunner.withUserConfiguration(DataSourceProxyConfiguration.class) this.contextRunner.withUserConfiguration(DataSourceProxyConfiguration.class)
.withPropertyValues( .withPropertyValues("spring.jmx.enabled=true",
"spring.datasource.type=" + HikariDataSource.class.getName(), "spring.datasource.type=" + HikariDataSource.class.getName(),
"spring.datasource.name=" + poolName, "spring.datasource.name=" + poolName,
"spring.datasource.hikari.register-mbeans=true") "spring.datasource.hikari.register-mbeans=true")

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -63,7 +63,8 @@ public class JmxAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(JmxAutoConfiguration.class); this.context.register(JmxAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(MBeanExporter.class)).isNotNull(); assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(MBeanExporter.class));
} }
@Test @Test

View File

@ -1217,8 +1217,9 @@ following example:
[[production-ready-jmx]] [[production-ready-jmx]]
== Monitoring and Management over JMX == Monitoring and Management over JMX
Java Management Extensions (JMX) provide a standard mechanism to monitor and manage Java Management Extensions (JMX) provide a standard mechanism to monitor and manage
applications. By default, Spring Boot exposes management endpoints as JMX MBeans under applications. By default, this feature is not enabled and can be turned on with
the `org.springframework.boot` domain. the configuration property `spring.jmx.enabled=true`. Spring Boot exposes
management endpoints as JMX MBeans under the `org.springframework.boot` domain by default.