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"/>
不知道为什么总是进不去这个拦截器中,也不抱任何错误!!!!
????????????????????
@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"/>
不知道为什么总是进不去这个拦截器中,也不抱任何错误!!!!
????????????????????
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货