Skip to content

Commit

Permalink
Remove use of @Autowired for configuration properties bean
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Mar 14, 2019
1 parent c4b8328 commit fcdc414
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class DefaultEndpointObjectNameFactory implements EndpointObjectNameFactory {

private final JmxEndpointProperties properties;

private final Environment environment;

private final MBeanServer mBeanServer;

private final String contextId;
Expand All @@ -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",
Expand All @@ -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()));
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -39,22 +36,14 @@ 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
* Endpoints.
*/
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit fcdc414

Please sign in to comment.