Spring AOP 动态多数据源的实例详解(2)

public class DataSourceAspect implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice { private static final Logger log = LogManager.getLogger(DataSourceAspect.class); public DataSourceAspect() { } public void before(Method m, Object[] args, Object target) throws Throwable { try { if(m != null) { if((m.getName().startsWith("list") || m.getName().startsWith("select") || m.getName().startsWith("get") || m.getName().startsWith("count")) && !m.getName().contains("FromMaster")) { DBContextHolder.setDbType("slave"); } else { DBContextHolder.setDbType("master"); } } } catch (Exception var5) { log.error("data source aspect error.", var5); } } public void after(JoinPoint point) { log.info("clear db type after method.current id {}", new Object[]{Long.valueOf(Thread.currentThread().getId())}); DBContextHolder.clearDBType(); } public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { } public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable { log.info("current db type {} when exception", new Object[]{DBContextHolder.getDbType()}); DBContextHolder.setDbType("master"); } }

以上就是 Spring AOP 动态多数据源的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjdws.html