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.transaction.TransactionManager; 020 021/** 022 * Interface to be implemented by @{@link org.springframework.context.annotation.Configuration 023 * Configuration} classes annotated with @{@link EnableTransactionManagement} that wish to 024 * (or need to) explicitly specify the default {@code PlatformTransactionManager} bean 025 * (or {@code ReactiveTransactionManager} bean) to be used for annotation-driven 026 * transaction management, as opposed to the default approach of a by-type lookup. 027 * One reason this might be necessary is if there are two {@code PlatformTransactionManager} 028 * beans (or two {@code ReactiveTransactionManager} beans) present in the container. 029 * 030 * <p>See @{@link EnableTransactionManagement} for general examples and context; 031 * see {@link #annotationDrivenTransactionManager()} for detailed instructions. 032 * 033 * <p>Note that in by-type lookup disambiguation cases, an alternative approach to 034 * implementing this interface is to simply mark one of the offending 035 * {@code PlatformTransactionManager} {@code @Bean} methods (or 036 * {@code ReactiveTransactionManager} {@code @Bean} methods) as 037 * {@link org.springframework.context.annotation.Primary @Primary}. 038 * This is even generally preferred since it doesn't lead to early initialization 039 * of the {@code TransactionManager} bean. 040 * 041 * @author Chris Beams 042 * @since 3.1 043 * @see EnableTransactionManagement 044 * @see org.springframework.context.annotation.Primary 045 * @see org.springframework.transaction.PlatformTransactionManager 046 * @see org.springframework.transaction.ReactiveTransactionManager 047 */ 048public interface TransactionManagementConfigurer { 049 050 /** 051 * Return the default transaction manager bean to use for annotation-driven database 052 * transaction management, i.e. when processing {@code @Transactional} methods. 053 * <p>There are two basic approaches to implementing this method: 054 * <h3>1. Implement the method and annotate it with {@code @Bean}</h3> 055 * In this case, the implementing {@code @Configuration} class implements this method, 056 * marks it with {@code @Bean}, and configures and returns the transaction manager 057 * directly within the method body: 058 * <pre class="code"> 059 * @Bean 060 * @Override 061 * public PlatformTransactionManager annotationDrivenTransactionManager() { 062 * return new DataSourceTransactionManager(dataSource()); 063 * }</pre> 064 * <h3>2. Implement the method without {@code @Bean} and delegate to another existing 065 * {@code @Bean} method</h3> 066 * <pre class="code"> 067 * @Bean 068 * public PlatformTransactionManager txManager() { 069 * return new DataSourceTransactionManager(dataSource()); 070 * } 071 * 072 * @Override 073 * public PlatformTransactionManager annotationDrivenTransactionManager() { 074 * return txManager(); // reference the existing {@code @Bean} method above 075 * }</pre> 076 * If taking approach #2, be sure that <em>only one</em> of the methods is marked 077 * with {@code @Bean}! 078 * <p>In either scenario #1 or #2, it is important that the 079 * {@code PlatformTransactionManager} instance is managed as a Spring bean within the 080 * container since most {@code PlatformTransactionManager} implementations take advantage 081 * of Spring lifecycle callbacks such as {@code InitializingBean} and 082 * {@code BeanFactoryAware}. Note that the same guidelines apply to 083 * {@code ReactiveTransactionManager} beans. 084 * @return a {@link org.springframework.transaction.PlatformTransactionManager} or 085 * {@link org.springframework.transaction.ReactiveTransactionManager} implementation 086 */ 087 TransactionManager annotationDrivenTransactionManager(); 088 089}