spring3 切换数据源进行读写分离,拦截不到org.springframework.jdbc.core.JdbcTemplate.*(..)

程序员Shark 程序员Shark 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
RT!!!!

aop实现代码


@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");

}

}



spring 配置文件


<aop:aspectj-autoproxy proxy-target-class="true"/>


不知道为什么总是进不去这个拦截器中,也不抱任何错误!!!!
????????????????????

...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
程序员Shark
Java EE 67,535 成员 225,843 内容