From fcdc414646ea0851002354b68618d7d608e30f27 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 14 Mar 2019 11:03:10 +0100 Subject: [PATCH] Remove use of `@Autowired` for configuration properties bean See gh-8762 --- .../jmx/DefaultEndpointObjectNameFactory.java | 13 ++++++++++++- .../endpoint/jmx/JmxEndpointProperties.java | 13 +------------ .../additional-spring-configuration-metadata.json | 4 ++++ .../autoconfigure/session/SessionProperties.java | 4 ++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactory.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactory.java index a03a8241c5d3..6e5f0ff27f0e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactory.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactory.java @@ -37,6 +37,8 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory { private final JmxEndpointProperties properties; + private final Environment environment; + private final MBeanServer mBeanServer; private final String contextId; @@ -46,6 +48,7 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory { DefaultEndpointObjectNameFactory(JmxEndpointProperties properties, Environment environment, MBeanServer mBeanServer, String contextId) { this.properties = properties; + this.environment = environment; this.mBeanServer = mBeanServer; this.contextId = contextId; this.uniqueNames = environment.getProperty("spring.jmx.unique-names", @@ -55,7 +58,7 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory { @Override public ObjectName getObjectName(ExposableJmxEndpoint endpoint) throws MalformedObjectNameException { - StringBuilder builder = new StringBuilder(this.properties.getDomain()); + StringBuilder builder = new StringBuilder(determineDomain()); builder.append(":type=Endpoint"); builder.append(",name=") .append(StringUtils.capitalize(endpoint.getEndpointId().toString())); @@ -71,6 +74,14 @@ public ObjectName getObjectName(ExposableJmxEndpoint endpoint) return ObjectNameManager.getInstance(builder.toString()); } + private String determineDomain() { + if (StringUtils.hasText(this.properties.getDomain())) { + return this.properties.getDomain(); + } + return this.environment.getProperty("spring.jmx.default-domain", + "org.springframework.boot"); + } + private boolean hasMBean(String baseObjectName) throws MalformedObjectNameException { ObjectName query = new ObjectName(baseObjectName + ",*"); return !this.mBeanServer.queryNames(query, null).isEmpty(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointProperties.java index 5cb5c25c3a1d..f0cdc513d958 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointProperties.java @@ -20,10 +20,7 @@ import java.util.Properties; import java.util.Set; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.Environment; -import org.springframework.util.StringUtils; /** * Configuration properties for JMX export of endpoints. @@ -39,7 +36,7 @@ public class JmxEndpointProperties { /** * Endpoints JMX domain name. Fallback to 'spring.jmx.default-domain' if set. */ - private String domain = "org.springframework.boot"; + private String domain; /** * Additional static properties to append to all ObjectNames of MBeans representing @@ -47,14 +44,6 @@ public class JmxEndpointProperties { */ private final Properties staticNames = new Properties(); - @Autowired - public JmxEndpointProperties(Environment environment) { - String defaultDomain = environment.getProperty("spring.jmx.default-domain"); - if (StringUtils.hasText(defaultDomain)) { - this.domain = defaultDomain; - } - } - public Exposure getExposure() { return this.exposure; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 0902cc4d37b5..f853bd532c90 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -33,6 +33,10 @@ "type": "java.lang.Boolean", "description": "Whether to enable or disable all endpoints by default." }, + { + "name": "management.endpoints.jmx.domain", + "defaultValue": "org.springframework.boot" + }, { "name": "management.endpoints.jmx.exposure.include", "defaultValue": "*" diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java index 64e7f2049f7c..d7fc783bd8eb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java @@ -57,10 +57,10 @@ public class SessionProperties { private Servlet servlet = new Servlet(); - private final ServerProperties serverProperties; + private ServerProperties serverProperties; @Autowired - public SessionProperties(ObjectProvider serverProperties) { + void setServerProperties(ObjectProvider serverProperties) { this.serverProperties = serverProperties.getIfUnique(); }