spring3 切换数据源进行读写分离,拦截不到org.springframework.jdbc.core.JdbcTemplate.*(..)
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"/>
不知道为什么总是进不去这个易做图中,也不抱任何错误!!!!
????????????????????
--------------------编程问答-------------------- 我觉得spring aop只能拦截受spring管理的bean的方法调用。而org.springframework.jdbc.core.JdbcTemplate.*(..)不是也未定义为spring bean。不过我对spring aop没有深入了解过,仅供参考。
补充:Java , Java EE