All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.config.datasource.dynamic.adapter.DataSourceMethodAdvice Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.datasource.dynamic.adapter;

import com.feingto.cloud.config.datasource.dynamic.DataSource;
import com.feingto.cloud.config.datasource.dynamic.support.DataSourceContext;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;

/**
 * 动态数据源切换前后置通知代理
 * TransactionSynchronizationManager.isCurrentTransactionReadOnly()
 *
 * @author longfei
 */
public class DataSourceMethodAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
    @Override
    public void before(Method method, @NonNull Object[] args, Object target) {
        String ds = method.getAnnotation(DataSource.class).value();
        if (DataSourceContext.contains(ds)) {
            DataSourceContext.setDbType(ds);
        }
    }

    @Override
    public void afterReturning(Object returnValue, @NonNull Method method, @NonNull Object[] args, Object target) {
        DataSourceContext.removeDbType();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy