package com.ylcm.sys.multiple;

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.springframework.stereotype.Component;

import com.ylcm.sys.annotation.DataSourceSelect;


/**
 * 
 * @ClassName: MultipleDataSourceAspectAdvice
 * @Description: 数据源切面
 * 
 */
@Aspect
@Component
public class MultipleDataSourceAspectAdvice {
	@Pointcut("@annotation(com.ylcm.sys.annotation.DataSourceSelect) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void dataSourceSelect() {
	}

	/**
	 * 
	 * @Description: 在加了DataSourceSelect注解的方法前设置数据源
	 * @param pjp
	 * @throws Throwable
	 */
	@Around(value = "dataSourceSelect() && @annotation(selectAnnotation)")
	public Object changeDataSource(ProceedingJoinPoint pjp, DataSourceSelect selectAnnotation) throws Throwable {
		MultipleDataSource.setDataSourceKey(selectAnnotation.value().getName());
		try {
			return pjp.proceed();
		} finally {
			MultipleDataSource.removeDataSourceKey();
		}
	}
}
