spring3 切换数据源进行读写分离,拦截不到org.springframework.jdbc.core.JdbcTemplate.*(..)
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 JDBC模板类—org.springframework.jdbc.core.JdbcTemplate
在实际开发中,我们通常通过Spring Bean配置来注入`JdbcTemplate`,如下所示:```xml<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate
Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法
首先,我们需要创建两个JdbcTemplate实例,分别对应两个数据源:```xml<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate
Spring框架JdbcTemplate类中查询方法介绍
Spring 框架 JdbcTemplate 类中查询方法介绍JdbcTemplate 是 Spring 框架中 org.springframework.jdbc.core 包提供的 JDBC 模板类
详解在spring中使用JdbcTemplate操作数据库的几种方式
创建JdbcTemplate实例后,可以设置数据源并执行SQL操作:```javaimport org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DriverManagerDataSource
4.Spring中的JdbcTemplate,Spring中的的事务,
" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/><


