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 * @since 3.1
034 * @see EnableTransactionManagement
035 * @see TransactionManagementConfigurationSelector
036 */
037@Configuration
038@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
039public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
040
041        @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
042        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
043        public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
044                BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
045                advisor.setTransactionAttributeSource(transactionAttributeSource());
046                advisor.setAdvice(transactionInterceptor());
047                advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
048                return advisor;
049        }
050
051        @Bean
052        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
053        public TransactionAttributeSource transactionAttributeSource() {
054                return new AnnotationTransactionAttributeSource();
055        }
056
057        @Bean
058        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
059        public TransactionInterceptor transactionInterceptor() {
060                TransactionInterceptor interceptor = new TransactionInterceptor();
061                interceptor.setTransactionAttributeSource(transactionAttributeSource());
062                if (this.txManager != null) {
063                        interceptor.setTransactionManager(this.txManager);
064                }
065                return interceptor;
066        }
067
068}