001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.jdbc; 018 019import javax.sql.DataSource; 020 021import org.springframework.boot.autoconfigure.AutoConfigureBefore; 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 026import org.springframework.boot.context.properties.EnableConfigurationProperties; 027import org.springframework.context.ApplicationContext; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; 031import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; 032import org.springframework.jmx.export.MBeanExporter; 033import org.springframework.jmx.support.JmxUtils; 034 035/** 036 * {@link EnableAutoConfiguration Auto-configuration} for a JNDI located 037 * {@link DataSource}. 038 * 039 * @author Phillip Webb 040 * @author Andy Wilkinson 041 * @since 1.2.0 042 */ 043@Configuration 044@AutoConfigureBefore({ XADataSourceAutoConfiguration.class, 045 DataSourceAutoConfiguration.class }) 046@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) 047@ConditionalOnProperty(prefix = "spring.datasource", name = "jndi-name") 048@EnableConfigurationProperties(DataSourceProperties.class) 049public class JndiDataSourceAutoConfiguration { 050 051 private final ApplicationContext context; 052 053 public JndiDataSourceAutoConfiguration(ApplicationContext context) { 054 this.context = context; 055 } 056 057 @Bean(destroyMethod = "") 058 @ConditionalOnMissingBean 059 public DataSource dataSource(DataSourceProperties properties) { 060 JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); 061 DataSource dataSource = dataSourceLookup.getDataSource(properties.getJndiName()); 062 excludeMBeanIfNecessary(dataSource, "dataSource"); 063 return dataSource; 064 } 065 066 private void excludeMBeanIfNecessary(Object candidate, String beanName) { 067 for (MBeanExporter mbeanExporter : this.context 068 .getBeansOfType(MBeanExporter.class).values()) { 069 if (JmxUtils.isMBean(candidate.getClass())) { 070 mbeanExporter.addExcludedBean(beanName); 071 } 072 } 073 } 074 075}