You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
66 lines
2.0 KiB
|
|
|
|
package com.xujie.sys.datasource.aspect;
|
|
|
|
|
|
import com.xujie.sys.datasource.config.DynamicContextHolder;
|
|
import com.xujie.sys.datasource.annotation.DataSource;
|
|
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.Ordered;
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
/**
|
|
* 多数据源,切面处理类
|
|
*
|
|
*
|
|
*/
|
|
@Aspect
|
|
@Component
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
public class DataSourceAspect {
|
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
@Pointcut("@annotation(com.xujie.sys.datasource.annotation.DataSource) " +
|
|
"|| @within(com.xujie.sys.datasource.annotation.DataSource)")
|
|
public void dataSourcePointCut() {
|
|
|
|
}
|
|
|
|
|
|
@Around("dataSourcePointCut()")
|
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
MethodSignature signature = (MethodSignature) point.getSignature();
|
|
Class targetClass = point.getTarget().getClass();
|
|
Method method = signature.getMethod();
|
|
|
|
DataSource targetDataSource = (DataSource)targetClass.getAnnotation(DataSource.class);
|
|
DataSource methodDataSource = method.getAnnotation(DataSource.class);
|
|
if(targetDataSource != null || methodDataSource != null){
|
|
String value;
|
|
if(methodDataSource != null){
|
|
value = methodDataSource.value();
|
|
}else {
|
|
value = targetDataSource.value();
|
|
}
|
|
|
|
DynamicContextHolder.push(value);
|
|
logger.debug("set datasource is {}", value);
|
|
}
|
|
|
|
try {
|
|
return point.proceed();
|
|
} finally {
|
|
DynamicContextHolder.poll();
|
|
logger.debug("clean datasource");
|
|
}
|
|
}
|
|
}
|