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