Merge branch '2.7.x'

This commit is contained in:
Scott Frederick
2022-03-18 16:52:47 -05:00
8 changed files with 181 additions and 137 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
@@ -22,7 +22,7 @@ import javax.management.ObjectName;
import org.springframework.boot.actuate.endpoint.jmx.EndpointObjectNameFactory;
import org.springframework.boot.actuate.endpoint.jmx.ExposableJmxEndpoint;
import org.springframework.core.env.Environment;
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.jmx.support.ObjectNameManager;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -37,21 +37,18 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory {
private final JmxEndpointProperties properties;
private final Environment environment;
private final JmxProperties jmxProperties;
private final MBeanServer mBeanServer;
private final String contextId;
private final boolean uniqueNames;
DefaultEndpointObjectNameFactory(JmxEndpointProperties properties, Environment environment, MBeanServer mBeanServer,
String contextId) {
DefaultEndpointObjectNameFactory(JmxEndpointProperties properties, JmxProperties jmxProperties,
MBeanServer mBeanServer, String contextId) {
this.properties = properties;
this.environment = environment;
this.jmxProperties = jmxProperties;
this.mBeanServer = mBeanServer;
this.contextId = contextId;
this.uniqueNames = environment.getProperty("spring.jmx.unique-names", Boolean.class, false);
}
@Override
@@ -63,7 +60,7 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory {
if (this.mBeanServer != null && hasMBean(baseName)) {
builder.append(",context=").append(this.contextId);
}
if (this.uniqueNames) {
if (this.jmxProperties.isUniqueNames()) {
String identity = ObjectUtils.getIdentityHexString(endpoint);
builder.append(",identity=").append(identity);
}
@@ -75,7 +72,10 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory {
if (StringUtils.hasText(this.properties.getDomain())) {
return this.properties.getDomain();
}
return this.environment.getProperty("spring.jmx.default-domain", "org.springframework.boot");
if (StringUtils.hasText(this.jmxProperties.getDefaultDomain())) {
return this.jmxProperties.getDefaultDomain();
}
return "org.springframework.boot";
}
private boolean hasMBean(String baseObjectName) throws MalformedObjectNameException {

View File

@@ -44,10 +44,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.util.ObjectUtils;
/**
@@ -60,7 +60,7 @@ import org.springframework.util.ObjectUtils;
* @since 2.0.0
*/
@AutoConfiguration(after = { JmxAutoConfiguration.class, EndpointAutoConfiguration.class })
@EnableConfigurationProperties(JmxEndpointProperties.class)
@EnableConfigurationProperties({ JmxEndpointProperties.class, JmxProperties.class })
@ConditionalOnProperty(prefix = "spring.jmx", name = "enabled", havingValue = "true")
public class JmxEndpointAutoConfiguration {
@@ -68,9 +68,13 @@ public class JmxEndpointAutoConfiguration {
private final JmxEndpointProperties properties;
public JmxEndpointAutoConfiguration(ApplicationContext applicationContext, JmxEndpointProperties properties) {
private final JmxProperties jmxProperties;
public JmxEndpointAutoConfiguration(ApplicationContext applicationContext, JmxEndpointProperties properties,
JmxProperties jmxProperties) {
this.applicationContext = applicationContext;
this.properties = properties;
this.jmxProperties = jmxProperties;
}
@Bean
@@ -85,10 +89,9 @@ public class JmxEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean(EndpointObjectNameFactory.class)
public DefaultEndpointObjectNameFactory endpointObjectNameFactory(MBeanServer mBeanServer,
Environment environment) {
public DefaultEndpointObjectNameFactory endpointObjectNameFactory(MBeanServer mBeanServer) {
String contextId = ObjectUtils.getIdentityHexString(this.applicationContext);
return new DefaultEndpointObjectNameFactory(this.properties, environment, mBeanServer, contextId);
return new DefaultEndpointObjectNameFactory(this.properties, this.jmxProperties, mBeanServer, contextId);
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
@@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.endpoint.EndpointId;
import org.springframework.boot.actuate.endpoint.jmx.ExposableJmxEndpoint;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,10 +40,10 @@ import static org.mockito.Mockito.mock;
*/
class DefaultEndpointObjectNameFactoryTests {
private final MockEnvironment environment = new MockEnvironment();
private final JmxEndpointProperties properties = new JmxEndpointProperties();
private final JmxProperties jmxProperties = new JmxProperties();
private final MBeanServer mBeanServer = mock(MBeanServer.class);
private String contextId;
@@ -69,7 +69,7 @@ class DefaultEndpointObjectNameFactoryTests {
@Test
void generateObjectNameWithUniqueNames() {
this.environment.setProperty("spring.jmx.unique-names", "true");
this.jmxProperties.setUniqueNames(true);
assertUniqueObjectName();
}
@@ -103,7 +103,7 @@ class DefaultEndpointObjectNameFactoryTests {
private ObjectName generateObjectName(ExposableJmxEndpoint endpoint) {
try {
return new DefaultEndpointObjectNameFactory(this.properties, this.environment, this.mBeanServer,
return new DefaultEndpointObjectNameFactory(this.properties, this.jmxProperties, this.mBeanServer,
this.contextId).getObjectName(endpoint);
}
catch (MalformedObjectNameException ex) {