001/*
002 * Copyright 2002-2020 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 *      https://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.transaction.annotation;
018
019import org.springframework.beans.factory.config.BeanDefinition;
020import org.springframework.context.annotation.Bean;
021import org.springframework.context.annotation.Configuration;
022import org.springframework.context.annotation.Role;
023import org.springframework.transaction.config.TransactionManagementConfigUtils;
024import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor;
025import org.springframework.transaction.interceptor.TransactionAttributeSource;
026import org.springframework.transaction.interceptor.TransactionInterceptor;
027
028/**
029 * {@code @Configuration} class that registers the Spring infrastructure beans
030 * necessary to enable proxy-based annotation-driven transaction management.
031 *
032 * @author Chris Beams
033 * @author Sebastien Deleuze
034 * @since 3.1
035 * @see EnableTransactionManagement
036 * @see TransactionManagementConfigurationSelector
037 */
038@Configuration(proxyBeanMethods = false)
039@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
040public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
041
042        @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
043        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
044        public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
045                        TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) {
046
047                BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
048                advisor.setTransactionAttributeSource(transactionAttributeSource);
049                advisor.setAdvice(transactionInterceptor);
050                if (this.enableTx != null) {
051                        advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
052                }
053                return advisor;
054        }
055
056        @Bean
057        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
058        public TransactionAttributeSource transactionAttributeSource() {
059                return new AnnotationTransactionAttributeSource();
060        }
061
062        @Bean
063        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
064        public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
065                TransactionInterceptor interceptor = new TransactionInterceptor();
066                interceptor.setTransactionAttributeSource(transactionAttributeSource);
067                if (this.txManager != null) {
068                        interceptor.setTransactionManager(this.txManager);
069                }
070                return interceptor;
071        }
072
073}