Use a ConfigurationProperties class for JMX properties

Closes gh-30327
This commit is contained in:
Scott Frederick
2022-03-18 14:40:22 -05:00
parent e5a09b3b31
commit a14f3ed200
7 changed files with 122 additions and 62 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandi
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration;
import org.springframework.boot.autoconfigure.sql.init.OnDatabaseInitializationCondition;
import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration;
@@ -46,7 +47,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableIntegrationManagement;
import org.springframework.integration.config.IntegrationManagementConfigurer;
@@ -84,7 +84,7 @@ import org.springframework.util.StringUtils;
@AutoConfiguration(after = { DataSourceAutoConfiguration.class, JmxAutoConfiguration.class,
TaskSchedulingAutoConfiguration.class })
@ConditionalOnClass(EnableIntegration.class)
@EnableConfigurationProperties(IntegrationProperties.class)
@EnableConfigurationProperties({ IntegrationProperties.class, JmxProperties.class })
public class IntegrationAutoConfiguration {
@Bean(name = IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)
@@ -186,14 +186,13 @@ public class IntegrationAutoConfiguration {
protected static class IntegrationJmxConfiguration {
@Bean
public IntegrationMBeanExporter integrationMbeanExporter(BeanFactory beanFactory, Environment environment) {
public IntegrationMBeanExporter integrationMbeanExporter(BeanFactory beanFactory, JmxProperties properties) {
IntegrationMBeanExporter exporter = new IntegrationMBeanExporter();
String defaultDomain = environment.getProperty("spring.jmx.default-domain");
String defaultDomain = properties.getDefaultDomain();
if (StringUtils.hasLength(defaultDomain)) {
exporter.setDefaultDomain(defaultDomain);
}
String serverBean = environment.getProperty("spring.jmx.server", "mbeanServer");
exporter.setServer(beanFactory.getBean(serverBean, MBeanServer.class));
exporter.setServer(beanFactory.getBean(properties.getServer(), MBeanServer.class));
return exporter;
}

View File

@@ -25,11 +25,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.MBeanExportConfiguration.SpecificPlatform;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
@@ -52,14 +52,15 @@ import org.springframework.util.StringUtils;
* @since 1.0.0
*/
@AutoConfiguration
@EnableConfigurationProperties(JmxProperties.class)
@ConditionalOnClass({ MBeanExporter.class })
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true")
public class JmxAutoConfiguration {
private final Environment environment;
private final JmxProperties properties;
public JmxAutoConfiguration(Environment environment) {
this.environment = environment;
public JmxAutoConfiguration(JmxProperties properties) {
this.properties = properties;
}
@Bean
@@ -69,12 +70,11 @@ public class JmxAutoConfiguration {
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
exporter.setNamingStrategy(namingStrategy);
String serverBean = this.environment.getProperty("spring.jmx.server", "mbeanServer");
String serverBean = this.properties.getServer();
if (StringUtils.hasLength(serverBean)) {
exporter.setServer(beanFactory.getBean(serverBean, MBeanServer.class));
}
boolean uniqueNames = this.environment.getProperty("spring.jmx.unique-names", Boolean.class, false);
exporter.setEnsureUniqueRuntimeObjectNames(uniqueNames);
exporter.setEnsureUniqueRuntimeObjectNames(this.properties.isUniqueNames());
return exporter;
}
@@ -82,12 +82,11 @@ public class JmxAutoConfiguration {
@ConditionalOnMissingBean(value = ObjectNamingStrategy.class, search = SearchStrategy.CURRENT)
public ParentAwareNamingStrategy objectNamingStrategy() {
ParentAwareNamingStrategy namingStrategy = new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
String defaultDomain = this.environment.getProperty("spring.jmx.default-domain");
String defaultDomain = this.properties.getDefaultDomain();
if (StringUtils.hasLength(defaultDomain)) {
namingStrategy.setDefaultDomain(defaultDomain);
}
boolean uniqueNames = this.environment.getProperty("spring.jmx.unique-names", Boolean.class, false);
namingStrategy.setEnsureUniqueRuntimeObjectNames(uniqueNames);
namingStrategy.setEnsureUniqueRuntimeObjectNames(this.properties.isUniqueNames());
return namingStrategy;
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2012-2022 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.autoconfigure.jmx;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for JMX.
*
* @author Scott Frederick
* @since 2.7.0
*/
@ConfigurationProperties(prefix = "spring.jmx")
public class JmxProperties {
/**
* Expose management beans to the JMX domain.
*/
private boolean enabled = false;
/**
* Whether unique runtime object names should be ensured.
*/
private boolean uniqueNames = false;
/**
* MBeanServer bean name.
*/
private String server = "mbeanServer";
/**
* JMX domain name.
*/
private String defaultDomain;
public boolean getEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isUniqueNames() {
return this.uniqueNames;
}
public void setUniqueNames(boolean uniqueNames) {
this.uniqueNames = uniqueNames;
}
public String getServer() {
return this.server;
}
public void setServer(String server) {
this.server = server;
}
public String getDefaultDomain() {
return this.defaultDomain;
}
public void setDefaultDomain(String defaultDomain) {
this.defaultDomain = defaultDomain;
}
}

View File

@@ -1120,29 +1120,6 @@
"name": "spring.jersey.type",
"defaultValue": "servlet"
},
{
"name": "spring.jmx.default-domain",
"type": "java.lang.String",
"description": "JMX domain name."
},
{
"name": "spring.jmx.enabled",
"type": "java.lang.Boolean",
"description": "Expose management beans to the JMX domain.",
"defaultValue": false
},
{
"name": "spring.jmx.server",
"type": "java.lang.String",
"description": "MBeanServer bean name.",
"defaultValue": "mbeanServer"
},
{
"name": "spring.jmx.unique-names",
"type": "java.lang.Boolean",
"description": "Whether unique runtime object names should be ensured.",
"defaultValue": false
},
{
"name": "spring.jpa.open-in-view",
"defaultValue": true