1. 程式人生 > 實用技巧 >騰訊追賬老乾媽後續:“一言難盡”背後的合作到底是什麼?

騰訊追賬老乾媽後續:“一言難盡”背後的合作到底是什麼?

http://www.caixin.com/2020-07-01/101574145.html


import java.util.Objects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 多資料來源處理
 * 
 */
@Aspect
@Order(1)
@Component
public class DataSourceAspect
{
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("@annotation(com.xxx.framework.aspectj.lang.annotation.DataSource)"
            + "|| @within(com.xxx.framework.aspectj.lang.annotation.DataSource)")
    public void dsPointCut()
    {

    }
     
     //環繞切面 執行前後
    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable
    {
        DataSource dataSource = getDataSource(point);

        if (StringUtils.isNotNull(dataSource))
        {
            DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
        }

        try
        {
            return point.proceed();
        }
        finally
        {
            // 銷燬資料來源 在執行方法之後
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
    }

    /**
     * 獲取需要切換的資料來源
     */
    public DataSource getDataSource(ProceedingJoinPoint point)
    {
        MethodSignature signature = (MethodSignature) point.getSignature();
        DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
        if (Objects.nonNull(dataSource))
        {
            return dataSource;
        }

        return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
    }
}