/* * BaseDynamicDataSourceHandlerImpl.java: This class is the base class for * all the handler classes which need the dynamic DataSource support. In each * subclass handling method, it should invoke the "setDataSourceContext(...)" * method to switch the DataSource(by passing the "mallId" argument from * the handling method's arguments). */ publicabstractclassBaseDynamicDataSourceHandlerImpl { ...
/** * Set the DataSource context according to the current mall. * * @param mallId */ publicvoidsetDataSourceContext(Integer mallId) { ... }
... }
/* * CustomerFlowHandlerImpl.java: A subclass of "BaseDynamicDataSourceHandlerImpl" * class. */ @Component publicclassCustomerFlowHandlerImplextendsBaseDynamicDataSourceHandlerImplimplementsCustomerFlowHandler { ...
/* * DynamicDataSourceAspect.java: The Aspect implementation which defines the pointcut * for each public method in any subclass of "BaseDynamicDataSourceHandlerImpl", and * makes the invocation of the "setDataSourceContext"method. */ @Component @Aspect publicclassDynamicDataSourceAspect { @Before("execution(public * com.jcloud.zhike.handler.impl.BaseDynamicDataSourceHandlerImpl+.*(..)) && args(mallId,..)") publicvoidsetDynamicDataSource(JoinPoint joinPoint, Integer mallId) { BaseDynamicDataSourceHandlerImpltarget= (BaseDynamicDataSourceHandlerImpl)joinPoint.getTarget(); target.setDataSourceContext(mallId); } }
/* * CustomerFlowHandlerImpl.java: Now the "setDataSourceContext(...)" invocation lines can be removed. */ @Component publicclassCustomerFlowHandlerImplextendsBaseDynamicDataSourceHandlerImplimplementsCustomerFlowHandler { ...