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 bec486f15d..a46406dac8 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 @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jdbc; import javax.sql.DataSource; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -25,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; @@ -48,8 +50,8 @@ import org.springframework.jmx.support.JmxUtils; @EnableConfigurationProperties(DataSourceProperties.class) public class JndiDataSourceAutoConfiguration { - @Autowired(required = false) - private MBeanExporter mbeanExporter; + @Autowired + private ApplicationContext context; @Bean(destroyMethod = "") @ConditionalOnMissingBean @@ -61,8 +63,14 @@ public class JndiDataSourceAutoConfiguration { } private void excludeMBeanIfNecessary(Object candidate, String beanName) { - if (this.mbeanExporter != null && JmxUtils.isMBean(candidate.getClass())) { - this.mbeanExporter.addExcludedBean(beanName); + try { + MBeanExporter mbeanExporter = this.context.getBean(MBeanExporter.class); + if (JmxUtils.isMBean(candidate.getClass())) { + mbeanExporter.addExcludedBean(beanName); + } + } + catch (NoSuchBeanDefinitionException ex) { + // No exporter. Exclusion is unnecessary } }