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