Skip to content

Commit 68fb578

Browse files
committed
Create one SpringApplicationAdminMXBeanRegistrar per context hierarchy
Previously, one SpringApplicationAdminMXBeanRegistrar was created per context. When there was more then one context this would result in a javax.management.InstanceAlreadyExistsException being thrown as an attempt was made to register the MBean more than once. This commit updates SpringApplicationAdminJmxAutoConfiguration so that the registrar is only created when there's no such existing bean in the context hierarchy. Closes gh-6378
1 parent 49302b3 commit 68fb578

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.admin.SpringApplicationAdminMXBean;
2323
import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar;
2424
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2627
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
2728
import org.springframework.context.annotation.Bean;
@@ -34,6 +35,7 @@
3435
* for internal use only.
3536
*
3637
* @author Stephane Nicoll
38+
* @author Andy Wilkinson
3739
* @since 1.3.0
3840
* @see SpringApplicationAdminMXBean
3941
*/
@@ -60,6 +62,7 @@ public class SpringApplicationAdminJmxAutoConfiguration {
6062
private Environment environment;
6163

6264
@Bean
65+
@ConditionalOnMissingBean
6366
public SpringApplicationAdminMXBeanRegistrar springApplicationAdminRegistrar()
6467
throws MalformedObjectNameException {
6568
String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY,

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,9 @@
3030
import org.junit.Test;
3131
import org.junit.rules.ExpectedException;
3232

33+
import org.springframework.beans.factory.BeanFactoryUtils;
34+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
35+
import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar;
3336
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
3437
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
3538
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
@@ -49,6 +52,7 @@
4952
* Tests for {@link SpringApplicationAdminJmxAutoConfiguration}.
5053
*
5154
* @author Stephane Nicoll
55+
* @author Andy Wilkinson
5256
*/
5357
public class SpringApplicationAdminJmxAutoConfigurationTests {
5458

@@ -131,6 +135,37 @@ public void registerWithSimpleWebApp() throws Exception {
131135
assertEquals(String.valueOf(expected), actual);
132136
}
133137

138+
@Test
139+
public void onlyRegisteredOnceWhenThereIsAChildContext() throws Exception {
140+
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder().web(false)
141+
.sources(JmxAutoConfiguration.class,
142+
SpringApplicationAdminJmxAutoConfiguration.class);
143+
SpringApplicationBuilder childBuilder = parentBuilder
144+
.child(JmxAutoConfiguration.class,
145+
SpringApplicationAdminJmxAutoConfiguration.class)
146+
.web(false);
147+
ConfigurableApplicationContext parent = null;
148+
ConfigurableApplicationContext child = null;
149+
150+
try {
151+
parent = parentBuilder.run("--" + ENABLE_ADMIN_PROP);
152+
child = childBuilder.run("--" + ENABLE_ADMIN_PROP);
153+
BeanFactoryUtils.beanOfType(parent.getBeanFactory(),
154+
SpringApplicationAdminMXBeanRegistrar.class);
155+
this.thrown.expect(NoSuchBeanDefinitionException.class);
156+
BeanFactoryUtils.beanOfType(child.getBeanFactory(),
157+
SpringApplicationAdminMXBeanRegistrar.class);
158+
}
159+
finally {
160+
if (parent != null) {
161+
parent.close();
162+
}
163+
if (child != null) {
164+
child.close();
165+
}
166+
}
167+
}
168+
134169
private ObjectName createDefaultObjectName() {
135170
return createObjectName(DEFAULT_JMX_NAME);
136171
}

0 commit comments

Comments
 (0)