001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
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 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
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 */
016package com.mybatisflex.spring.boot;
017
018import com.mybatisflex.core.row.Db;
019import com.mybatisflex.spring.FlexTransactionManager;
020import org.springframework.boot.autoconfigure.AutoConfigureAfter;
021import org.springframework.boot.autoconfigure.AutoConfigureBefore;
022import org.springframework.boot.autoconfigure.AutoConfigureOrder;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
025import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
026import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.core.Ordered;
030import org.springframework.lang.NonNull;
031import org.springframework.transaction.PlatformTransactionManager;
032import org.springframework.transaction.TransactionManager;
033import org.springframework.transaction.annotation.TransactionManagementConfigurer;
034
035/**
036 * MyBatis-Flex 事务自动配置。
037 *
038 * @author michael
039 */
040@ConditionalOnClass(Db.class)
041@ConditionalOnMissingBean(TransactionManager.class)
042@Configuration(proxyBeanMethods = false)
043@AutoConfigureOrder(FlexTransactionAutoConfiguration.PRECEDENCE)
044@AutoConfigureAfter({MybatisFlexAutoConfiguration.class})
045@AutoConfigureBefore({TransactionAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
046public class FlexTransactionAutoConfiguration implements TransactionManagementConfigurer {
047
048    /**
049     * 优先级
050     */
051    protected static final int PRECEDENCE = Ordered.LOWEST_PRECEDENCE - 10;
052
053    /**
054     * 这里使用 final 修饰属性是因为:<br>
055     * <p>
056     * 1、调用 {@link #annotationDrivenTransactionManager} 方法会返回 TransactionManager 对象<br>
057     * 2、{@code @Bean} 注入又会返回 TransactionManager 对象<br>
058     * <p>
059     * 需要保证两个对象的一致性。
060     */
061    private final FlexTransactionManager flexTransactionManager = new FlexTransactionManager();
062
063    @NonNull
064    @Override
065    @Bean(name = "transactionManager")
066    public PlatformTransactionManager annotationDrivenTransactionManager() {
067        return flexTransactionManager;
068    }
069
070}