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

飓风zj 2012-05-18 10:39:12
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"/>


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

...全文
243 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cseu 2012-05-18
  • 打赏
  • 举报
回复
我觉得spring aop只能拦截受spring管理的bean的方法调用。而org.springframework.jdbc.core.JdbcTemplate.*(..)不是也未定义为spring bean。不过我对spring aop没有深入了解过,仅供参考。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧