001/* 002 * Copyright 2002-2019 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 java.util.Collection; 020 021import org.springframework.beans.factory.annotation.Autowired; 022import org.springframework.beans.factory.config.BeanDefinition; 023import org.springframework.context.annotation.Bean; 024import org.springframework.context.annotation.Configuration; 025import org.springframework.context.annotation.ImportAware; 026import org.springframework.context.annotation.Role; 027import org.springframework.core.annotation.AnnotationAttributes; 028import org.springframework.core.type.AnnotationMetadata; 029import org.springframework.lang.Nullable; 030import org.springframework.transaction.TransactionManager; 031import org.springframework.transaction.config.TransactionManagementConfigUtils; 032import org.springframework.transaction.event.TransactionalEventListenerFactory; 033import org.springframework.util.CollectionUtils; 034 035/** 036 * Abstract base {@code @Configuration} class providing common structure for enabling 037 * Spring's annotation-driven transaction management capability. 038 * 039 * @author Chris Beams 040 * @author Stephane Nicoll 041 * @since 3.1 042 * @see EnableTransactionManagement 043 */ 044@Configuration 045public abstract class AbstractTransactionManagementConfiguration implements ImportAware { 046 047 @Nullable 048 protected AnnotationAttributes enableTx; 049 050 /** 051 * Default transaction manager, as configured through a {@link TransactionManagementConfigurer}. 052 */ 053 @Nullable 054 protected TransactionManager txManager; 055 056 057 @Override 058 public void setImportMetadata(AnnotationMetadata importMetadata) { 059 this.enableTx = AnnotationAttributes.fromMap( 060 importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false)); 061 if (this.enableTx == null) { 062 throw new IllegalArgumentException( 063 "@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName()); 064 } 065 } 066 067 @Autowired(required = false) 068 void setConfigurers(Collection<TransactionManagementConfigurer> configurers) { 069 if (CollectionUtils.isEmpty(configurers)) { 070 return; 071 } 072 if (configurers.size() > 1) { 073 throw new IllegalStateException("Only one TransactionManagementConfigurer may exist"); 074 } 075 TransactionManagementConfigurer configurer = configurers.iterator().next(); 076 this.txManager = configurer.annotationDrivenTransactionManager(); 077 } 078 079 080 @Bean(name = TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME) 081 @Role(BeanDefinition.ROLE_INFRASTRUCTURE) 082 public static TransactionalEventListenerFactory transactionalEventListenerFactory() { 083 return new TransactionalEventListenerFactory(); 084 } 085 086}