Skip to content

Commit ffdff1c

Browse files
committed
Consider multiple MBeanExporters when excluding beans from export
Closes gh-10632
1 parent 8a653d2 commit ffdff1c

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.admin;
1818

19+
import java.util.List;
20+
1921
import javax.management.MalformedObjectNameException;
2022

2123
import org.springframework.beans.factory.ObjectProvider;
@@ -55,13 +57,13 @@ public class SpringApplicationAdminJmxAutoConfiguration {
5557
*/
5658
private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
5759

58-
private final MBeanExporter mbeanExporter;
60+
private final List<MBeanExporter> mbeanExporters;
5961

6062
private final Environment environment;
6163

6264
public SpringApplicationAdminJmxAutoConfiguration(
63-
ObjectProvider<MBeanExporter> mbeanExporter, Environment environment) {
64-
this.mbeanExporter = mbeanExporter.getIfAvailable();
65+
ObjectProvider<List<MBeanExporter>> mbeanExporters, Environment environment) {
66+
this.mbeanExporters = mbeanExporters.getIfAvailable();
6567
this.environment = environment;
6668
}
6769

@@ -71,8 +73,10 @@ public SpringApplicationAdminMXBeanRegistrar springApplicationAdminRegistrar()
7173
throws MalformedObjectNameException {
7274
String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY,
7375
DEFAULT_JMX_NAME);
74-
if (this.mbeanExporter != null) { // Make sure to not register that MBean twice
75-
this.mbeanExporter.addExcludedBean(jmxName);
76+
if (this.mbeanExporters != null) { // Make sure to not register that MBean twice
77+
for (MBeanExporter mbeanExporter : this.mbeanExporters) {
78+
mbeanExporter.addExcludedBean(jmxName);
79+
}
7680
}
7781
return new SpringApplicationAdminMXBeanRegistrar(jmxName);
7882
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -18,7 +18,6 @@
1818

1919
import javax.sql.DataSource;
2020

21-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2221
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2322
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2423
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -65,15 +64,12 @@ public DataSource dataSource(DataSourceProperties properties) {
6564
}
6665

6766
private void excludeMBeanIfNecessary(Object candidate, String beanName) {
68-
try {
69-
MBeanExporter mbeanExporter = this.context.getBean(MBeanExporter.class);
67+
for (MBeanExporter mbeanExporter : this.context
68+
.getBeansOfType(MBeanExporter.class).values()) {
7069
if (JmxUtils.isMBean(candidate.getClass())) {
7170
mbeanExporter.addExcludedBean(beanName);
7271
}
7372
}
74-
catch (NoSuchBeanDefinitionException ex) {
75-
// No exporter. Exclusion is unnecessary
76-
}
7773
}
7874

7975
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -42,6 +42,9 @@
4242
import org.springframework.boot.test.util.EnvironmentTestUtils;
4343
import org.springframework.context.ConfigurableApplicationContext;
4444
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
45+
import org.springframework.context.annotation.Bean;
46+
import org.springframework.context.annotation.Configuration;
47+
import org.springframework.jmx.export.MBeanExporter;
4548

4649
import static org.assertj.core.api.Assertions.assertThat;
4750
import static org.junit.Assert.fail;
@@ -186,10 +189,25 @@ private String getProperty(ObjectName objectName, String key) throws Exception {
186189
private void load(String... environment) {
187190
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
188191
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
189-
applicationContext.register(JmxAutoConfiguration.class,
192+
applicationContext.register(MultipleMBeanExportersConfiguration.class,
190193
SpringApplicationAdminJmxAutoConfiguration.class);
191194
applicationContext.refresh();
192195
this.context = applicationContext;
193196
}
194197

198+
@Configuration
199+
static class MultipleMBeanExportersConfiguration {
200+
201+
@Bean
202+
public MBeanExporter firstMBeanExporter() {
203+
return new MBeanExporter();
204+
}
205+
206+
@Bean
207+
public MBeanExporter secondMBeanExporter() {
208+
return new MBeanExporter();
209+
}
210+
211+
}
212+
195213
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -117,6 +117,30 @@ public void mbeanDataSourceIsExcludedFromExport()
117117
assertThat(excludedBeans).containsExactly("dataSource");
118118
}
119119

120+
@SuppressWarnings("unchecked")
121+
@Test
122+
public void mbeanDataSourceIsExcludedFromExportByAllExporters()
123+
throws IllegalStateException, NamingException {
124+
DataSource dataSource = new BasicDataSource();
125+
configureJndi("foo", dataSource);
126+
127+
this.context = new AnnotationConfigApplicationContext();
128+
EnvironmentTestUtils.addEnvironment(this.context,
129+
"spring.datasource.jndi-name:foo");
130+
this.context.register(JndiDataSourceAutoConfiguration.class,
131+
MBeanExporterConfiguration.class,
132+
AnotherMBeanExporterConfiguration.class);
133+
this.context.refresh();
134+
135+
assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource);
136+
for (MBeanExporter exporter : this.context.getBeansOfType(MBeanExporter.class)
137+
.values()) {
138+
Set<String> excludedBeans = (Set<String>) new DirectFieldAccessor(exporter)
139+
.getPropertyValue("excludedBeans");
140+
assertThat(excludedBeans).containsExactly("dataSource");
141+
}
142+
}
143+
120144
@SuppressWarnings("unchecked")
121145
@Test
122146
public void standardDataSourceIsNotExcludedFromExport()
@@ -152,4 +176,13 @@ MBeanExporter mbeanExporter() {
152176

153177
}
154178

179+
private static class AnotherMBeanExporterConfiguration {
180+
181+
@Bean
182+
MBeanExporter anotherMbeanExporter() {
183+
return new MBeanExporter();
184+
}
185+
186+
}
187+
155188
}

0 commit comments

Comments
 (0)