diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java index 1119d094aab..c21f7ef4068 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.admin; +import java.util.List; + import javax.management.MalformedObjectNameException; import org.springframework.beans.factory.ObjectProvider; @@ -55,13 +57,13 @@ public class SpringApplicationAdminJmxAutoConfiguration { */ private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication"; - private final MBeanExporter mbeanExporter; + private final List mbeanExporters; private final Environment environment; public SpringApplicationAdminJmxAutoConfiguration( - ObjectProvider mbeanExporter, Environment environment) { - this.mbeanExporter = mbeanExporter.getIfAvailable(); + ObjectProvider> mbeanExporters, Environment environment) { + this.mbeanExporters = mbeanExporters.getIfAvailable(); this.environment = environment; } @@ -71,8 +73,10 @@ public class SpringApplicationAdminJmxAutoConfiguration { throws MalformedObjectNameException { String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY, DEFAULT_JMX_NAME); - if (this.mbeanExporter != null) { // Make sure to not register that MBean twice - this.mbeanExporter.addExcludedBean(jmxName); + if (this.mbeanExporters != null) { // Make sure to not register that MBean twice + for (MBeanExporter mbeanExporter : this.mbeanExporters) { + mbeanExporter.addExcludedBean(jmxName); + } } return new SpringApplicationAdminMXBeanRegistrar(jmxName); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java index 871d1561826..dcfd3a9a28d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.jdbc; import javax.sql.DataSource; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -65,15 +64,12 @@ public class JndiDataSourceAutoConfiguration { } private void excludeMBeanIfNecessary(Object candidate, String beanName) { - try { - MBeanExporter mbeanExporter = this.context.getBean(MBeanExporter.class); + for (MBeanExporter mbeanExporter : this.context + .getBeansOfType(MBeanExporter.class).values()) { if (JmxUtils.isMBean(candidate.getClass())) { mbeanExporter.addExcludedBean(beanName); } } - catch (NoSuchBeanDefinitionException ex) { - // No exporter. Exclusion is unnecessary - } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java index de95903cfad..45414c75413 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,9 @@ import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jmx.export.MBeanExporter; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -186,10 +189,25 @@ public class SpringApplicationAdminJmxAutoConfigurationTests { private void load(String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(applicationContext, environment); - applicationContext.register(JmxAutoConfiguration.class, + applicationContext.register(MultipleMBeanExportersConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class); applicationContext.refresh(); this.context = applicationContext; } + @Configuration + static class MultipleMBeanExportersConfiguration { + + @Bean + public MBeanExporter firstMBeanExporter() { + return new MBeanExporter(); + } + + @Bean + public MBeanExporter secondMBeanExporter() { + return new MBeanExporter(); + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java index e1ceb09b7dd..fedc3e1611e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -117,6 +117,30 @@ public class JndiDataSourceAutoConfigurationTests { assertThat(excludedBeans).containsExactly("dataSource"); } + @SuppressWarnings("unchecked") + @Test + public void mbeanDataSourceIsExcludedFromExportByAllExporters() + throws IllegalStateException, NamingException { + DataSource dataSource = new BasicDataSource(); + configureJndi("foo", dataSource); + + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.datasource.jndi-name:foo"); + this.context.register(JndiDataSourceAutoConfiguration.class, + MBeanExporterConfiguration.class, + AnotherMBeanExporterConfiguration.class); + this.context.refresh(); + + assertThat(this.context.getBean(DataSource.class)).isEqualTo(dataSource); + for (MBeanExporter exporter : this.context.getBeansOfType(MBeanExporter.class) + .values()) { + Set excludedBeans = (Set) new DirectFieldAccessor(exporter) + .getPropertyValue("excludedBeans"); + assertThat(excludedBeans).containsExactly("dataSource"); + } + } + @SuppressWarnings("unchecked") @Test public void standardDataSourceIsNotExcludedFromExport() @@ -152,4 +176,13 @@ public class JndiDataSourceAutoConfigurationTests { } + private static class AnotherMBeanExporterConfiguration { + + @Bean + MBeanExporter anotherMbeanExporter() { + return new MBeanExporter(); + } + + } + }