67,512
社区成员
发帖
与我相关
我的任务
分享
@Component
@Aspect
public class DataSourceInterceptor {
private static Logger log = Logger.getLogger(DataSourceInterceptor.class);
@Before("dtSourcePointcut()")
public void doBefore() {
System.out.println("前置通知:");
}
//定义切面
@Pointcut("execution(* org.springframework.jdbc.core.JdbcTemplate.*(..))")
public void dtSourcePointcut() {
}
@Around("dtSourcePointcut()")
public Object doAround(ProceedingJoinPoint call){
Object rs = null;
try {
Signature signature = call.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
String name = method.getName();
if(name.startsWith("update") || name.startsWith("batchUpdate")){
this.setMasterDataSource();
}else{
this.setSlaveDataSource();
}
rs = call.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
return rs;
}
public void setMasterDataSource() {
CustomerContextHolder.setCustomerType("master");
log.info("DataSource=====>master");
}
public void setSlaveDataSource() {
CustomerContextHolder.setCustomerType("slave");
log.info("DataSource=====>slave");
}
}
<aop:aspectj-autoproxy proxy-target-class="true"/>