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.transaction; 018 019import java.util.List; 020 021import org.springframework.beans.factory.ObjectProvider; 022import org.springframework.boot.autoconfigure.AutoConfigureAfter; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; 028import org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration; 029import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; 030import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 031import org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration; 032import org.springframework.boot.context.properties.EnableConfigurationProperties; 033import org.springframework.context.annotation.Bean; 034import org.springframework.context.annotation.Configuration; 035import org.springframework.transaction.PlatformTransactionManager; 036import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; 037import org.springframework.transaction.annotation.EnableTransactionManagement; 038import org.springframework.transaction.support.TransactionTemplate; 039 040/** 041 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration 042 * Auto-configuration} for Spring transaction. 043 * 044 * @author Stephane Nicoll 045 * @since 1.3.0 046 */ 047@Configuration 048@ConditionalOnClass(PlatformTransactionManager.class) 049@AutoConfigureAfter({ JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, 050 DataSourceTransactionManagerAutoConfiguration.class, 051 Neo4jDataAutoConfiguration.class }) 052@EnableConfigurationProperties(TransactionProperties.class) 053public class TransactionAutoConfiguration { 054 055 @Bean 056 @ConditionalOnMissingBean 057 public TransactionManagerCustomizers platformTransactionManagerCustomizers( 058 ObjectProvider<List<PlatformTransactionManagerCustomizer<?>>> customizers) { 059 return new TransactionManagerCustomizers(customizers.getIfAvailable()); 060 } 061 062 @Configuration 063 @ConditionalOnSingleCandidate(PlatformTransactionManager.class) 064 public static class TransactionTemplateConfiguration { 065 066 private final PlatformTransactionManager transactionManager; 067 068 public TransactionTemplateConfiguration( 069 PlatformTransactionManager transactionManager) { 070 this.transactionManager = transactionManager; 071 } 072 073 @Bean 074 @ConditionalOnMissingBean 075 public TransactionTemplate transactionTemplate() { 076 return new TransactionTemplate(this.transactionManager); 077 } 078 } 079 080 @Configuration 081 @ConditionalOnBean(PlatformTransactionManager.class) 082 @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class) 083 public static class EnableTransactionManagementConfiguration { 084 085 @Configuration 086 @EnableTransactionManagement(proxyTargetClass = false) 087 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false) 088 public static class JdkDynamicAutoProxyConfiguration { 089 090 } 091 092 @Configuration 093 @EnableTransactionManagement(proxyTargetClass = true) 094 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true) 095 public static class CglibAutoProxyConfiguration { 096 097 } 098 099 } 100 101}