From ce9626d00fcaf12621278ac0a008813f61cbbff3 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 5 Mar 2019 15:30:19 +0100 Subject: [PATCH] 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 --- .../JmxEndpointIntegrationTests.java | 2 +- .../KafkaMetricsAutoConfigurationTests.java | 3 +- .../jmx/JmxAutoConfiguration.java | 4 +- ...itional-spring-configuration-metadata.json | 2 +- .../IntegrationAutoConfigurationTests.java | 41 +++++++++---------- .../jdbc/DataSourceJmxConfigurationTests.java | 6 +-- .../jmx/JmxAutoConfigurationTests.java | 5 ++- .../asciidoc/production-ready-features.adoc | 5 ++- 8 files changed, 35 insertions(+), 33 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/JmxEndpointIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/JmxEndpointIntegrationTests.java index d81cda552128..1c3d9775cdbb 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/JmxEndpointIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/JmxEndpointIntegrationTests.java @@ -50,7 +50,7 @@ public class JmxEndpointIntegrationTests { EndpointAutoConfiguration.class, JmxEndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class, HttpTraceAutoConfiguration.class)) - .withConfiguration( + .withPropertyValues("spring.jmx.enabled=true").withConfiguration( AutoConfigurations.of(EndpointAutoConfigurationClasses.ALL)); @Test diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/KafkaMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/KafkaMetricsAutoConfigurationTests.java index 227eb3cb1aa9..3fe74427dc2d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/KafkaMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/KafkaMetricsAutoConfigurationTests.java @@ -36,7 +36,8 @@ public class KafkaMetricsAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .with(MetricsRun.simple()).withConfiguration( + .with(MetricsRun.simple()).withPropertyValues("spring.jmx.enabled=true") + .withConfiguration( AutoConfigurations.of(KafkaMetricsAutoConfiguration.class)); @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java index 3f297b3b44d9..1de92f0bc300 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java @@ -42,7 +42,7 @@ * {@link EnableAutoConfiguration Auto-configuration} to enable/disable Spring's * {@link EnableMBeanExport} mechanism based on configuration properties. *

- * 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 Madhura Bhave @@ -50,7 +50,7 @@ */ @Configuration @ConditionalOnClass({ MBeanExporter.class }) -@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true") public class JmxAutoConfiguration { private final Environment environment; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 5a4d21307774..19f06f3c5210 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -463,7 +463,7 @@ "name": "spring.jmx.enabled", "type": "java.lang.Boolean", "description": "Expose management beans to the JMX domain.", - "defaultValue": true + "defaultValue": false }, { "name": "spring.jmx.server", diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java index aed522e92a0c..f42edc72c50b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -101,31 +101,30 @@ public void parentContext() { } @Test - public void jmxIntegrationEnabledByDefault() { - this.contextRunner.run((context) -> { - MBeanServer mBeanServer = context.getBean(MBeanServer.class); - assertThat(mBeanServer.getDomains()).contains( - "org.springframework.integration", - "org.springframework.integration.monitor"); - assertThat(context) - .hasBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME); - }); + public void enableJmxIntegration() { + this.contextRunner.withPropertyValues("spring.jmx.enabled=true") + .run((context) -> { + 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) -> { - assertThat(context).doesNotHaveBean(MBeanServer.class); - assertThat(context) - .hasSingleBean(IntegrationManagementConfigurer.class); - }); + public void jmxIntegrationIsDisabledByDefault() { + this.contextRunner.run((context) -> { + assertThat(context).doesNotHaveBean(MBeanServer.class); + assertThat(context).hasSingleBean(IntegrationManagementConfigurer.class); + }); } @Test public void customizeJmxDomain() { - this.contextRunner.withPropertyValues("spring.jmx.default_domain=org.foo") - .run((context) -> { + this.contextRunner.withPropertyValues("spring.jmx.enabled=true", + "spring.jmx.default_domain=org.foo").run((context) -> { MBeanServer mBeanServer = context.getBean(MBeanServer.class); assertThat(mBeanServer.getDomains()).contains("org.foo") .doesNotContain("org.springframework.integration", @@ -135,8 +134,8 @@ public void customizeJmxDomain() { @Test public void primaryExporterIsAllowed() { - this.contextRunner.withUserConfiguration(CustomMBeanExporter.class) - .run((context) -> { + this.contextRunner.withPropertyValues("spring.jmx.enabled=true") + .withUserConfiguration(CustomMBeanExporter.class).run((context) -> { assertThat(context).getBeans(MBeanExporter.class).hasSize(2); assertThat(context.getBean(MBeanExporter.class)) .isSameAs(context.getBean("myMBeanExporter")); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java index 38e5870abca6..ca0952a8cfd2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -60,7 +60,7 @@ public class DataSourceJmxConfigurationTests { public void hikariAutoConfiguredCanUseRegisterMBeans() { String poolName = UUID.randomUUID().toString(); this.contextRunner - .withPropertyValues( + .withPropertyValues("spring.jmx.enabled=true", "spring.datasource.type=" + HikariDataSource.class.getName(), "spring.datasource.name=" + poolName, "spring.datasource.hikari.register-mbeans=true") @@ -118,7 +118,7 @@ public void hikariAutoConfiguredUsesJmsFlag() { public void hikariProxiedCanUseRegisterMBeans() { String poolName = UUID.randomUUID().toString(); this.contextRunner.withUserConfiguration(DataSourceProxyConfiguration.class) - .withPropertyValues( + .withPropertyValues("spring.jmx.enabled=true", "spring.datasource.type=" + HikariDataSource.class.getName(), "spring.datasource.name=" + poolName, "spring.datasource.hikari.register-mbeans=true") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java index f1308f68b75a..61c7ba9ce242 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -63,7 +63,8 @@ public void testDefaultMBeanExport() { this.context = new AnnotationConfigApplicationContext(); this.context.register(JmxAutoConfiguration.class); this.context.refresh(); - assertThat(this.context.getBean(MBeanExporter.class)).isNotNull(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(MBeanExporter.class)); } @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 26dd7cce2ff7..5d9e7f736d45 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -1217,8 +1217,9 @@ following example: [[production-ready-jmx]] == Monitoring and Management over JMX Java Management Extensions (JMX) provide a standard mechanism to monitor and manage -applications. By default, Spring Boot exposes management endpoints as JMX MBeans under -the `org.springframework.boot` domain. +applications. By default, this feature is not enabled and can be turned on with +the configuration property `spring.jmx.enabled=true`. Spring Boot exposes +management endpoints as JMX MBeans under the `org.springframework.boot` domain by default.