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.beans.factory.ObjectProvider; 022import org.springframework.boot.autoconfigure.AutoConfigureOrder; 023import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 027import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; 028import org.springframework.boot.context.properties.EnableConfigurationProperties; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Configuration; 031import org.springframework.core.Ordered; 032import org.springframework.jdbc.core.JdbcTemplate; 033import org.springframework.jdbc.datasource.DataSourceTransactionManager; 034import org.springframework.transaction.PlatformTransactionManager; 035 036/** 037 * {@link EnableAutoConfiguration Auto-configuration} for 038 * {@link DataSourceTransactionManager}. 039 * 040 * @author Dave Syer 041 * @author Stephane Nicoll 042 * @author Andy Wilkinson 043 * @author Kazuki Shimizu 044 */ 045@Configuration 046@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class }) 047@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE) 048@EnableConfigurationProperties(DataSourceProperties.class) 049public class DataSourceTransactionManagerAutoConfiguration { 050 051 @Configuration 052 @ConditionalOnSingleCandidate(DataSource.class) 053 static class DataSourceTransactionManagerConfiguration { 054 055 private final DataSource dataSource; 056 057 private final TransactionManagerCustomizers transactionManagerCustomizers; 058 059 DataSourceTransactionManagerConfiguration(DataSource dataSource, 060 ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) { 061 this.dataSource = dataSource; 062 this.transactionManagerCustomizers = transactionManagerCustomizers 063 .getIfAvailable(); 064 } 065 066 @Bean 067 @ConditionalOnMissingBean(PlatformTransactionManager.class) 068 public DataSourceTransactionManager transactionManager( 069 DataSourceProperties properties) { 070 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager( 071 this.dataSource); 072 if (this.transactionManagerCustomizers != null) { 073 this.transactionManagerCustomizers.customize(transactionManager); 074 } 075 return transactionManager; 076 } 077 078 } 079 080}