Remove use of @Autowired for configuration properties bean

See gh-8762
This commit is contained in:
Stephane Nicoll 2019-03-14 11:03:10 +01:00
parent c4b8328a2b
commit fcdc414646
4 changed files with 19 additions and 15 deletions

View File

@ -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 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory {
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();

View File

@ -20,10 +20,7 @@ import java.util.LinkedHashSet;
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;
}

View File

@ -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": "*"

View File

@ -57,10 +57,10 @@ public class SessionProperties {
private Servlet servlet = new Servlet();
private final ServerProperties serverProperties;
private ServerProperties serverProperties;
@Autowired
public SessionProperties(ObjectProvider<ServerProperties> serverProperties) {
void setServerProperties(ObjectProvider<ServerProperties> serverProperties) {
this.serverProperties = serverProperties.getIfUnique();
}