001/*
002 * Copyright 2012-2018 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.stream.Collectors;
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<PlatformTransactionManagerCustomizer<?>> customizers) {
059                return new TransactionManagerCustomizers(
060                                customizers.orderedStream().collect(Collectors.toList()));
061        }
062
063        @Configuration
064        @ConditionalOnSingleCandidate(PlatformTransactionManager.class)
065        public static class TransactionTemplateConfiguration {
066
067                private final PlatformTransactionManager transactionManager;
068
069                public TransactionTemplateConfiguration(
070                                PlatformTransactionManager transactionManager) {
071                        this.transactionManager = transactionManager;
072                }
073
074                @Bean
075                @ConditionalOnMissingBean
076                public TransactionTemplate transactionTemplate() {
077                        return new TransactionTemplate(this.transactionManager);
078                }
079
080        }
081
082        @Configuration
083        @ConditionalOnBean(PlatformTransactionManager.class)
084        @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
085        public static class EnableTransactionManagementConfiguration {
086
087                @Configuration
088                @EnableTransactionManagement(proxyTargetClass = false)
089                @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
090                public static class JdkDynamicAutoProxyConfiguration {
091
092                }
093
094                @Configuration
095                @EnableTransactionManagement(proxyTargetClass = true)
096                @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
097                public static class CglibAutoProxyConfiguration {
098
099                }
100
101        }
102
103}