標籤:spring Aop @AfterReturning MySQL和Redis 資料同步
1、定義一個切面,使用AfterReturning通知,修改、刪除、新增等成功後更新緩衝
a、修改時先改資料庫資料,修改成功後再同步到緩衝中、
b、刪除時先刪除資料庫資料,刪除成功再清理緩衝中的對應資料
c、新增時先插入資料庫,插入成功,再同步進入緩衝
com.aspect.demo;com.model.Company;com.model.ExceptionCompany;com.Cache.RedisCache;org.aspectj.lang.JoinPoint;org.aspectj.lang.annotation.*;org.slf4j.Logger;org.slf4j.LoggerFactory;;java.util.List;ReceiveCacheManager { //緩衝管理類用來擷取緩衝中的單位資訊資料 RedisCache ; setRedisCache(RedisCache redisCache) { .= redisCache; } Logger = LoggerFactory.(ReceiveCacheManager.); () insertService() { } () updateService() { } () deleteService() { } () insertCompany(JoinPoint joinPoint) { {
//擷取方法參數 Object[] args = joinPoint.getArgs(); (args != && args.> && args[].getClass() == Company.) { Company company = (Company) args[]; (.exists()){ Object object = .get(); (object == ){}{ List<ExceptionCompany> list = (List<ExceptionCompany>) object; ExceptionCompany exceptionCompany = ExceptionCompany(); exceptionCompany.setBusinessLicense(company.getBusinessLicense()); exceptionCompany.setCompanyName(company.getCompanyName()); exceptionCompany.setCompanyAddress(company.getCompanyAddress()); exceptionCompany.setLinkMan(company.getLinkMan()); exceptionCompany.setLinkTel(company.getLinkTel()); exceptionCompany.setOrganizationCode(company.getOrganizationCode()); exceptionCompany.setTaxRegistration(company.getTaxRegistration()); list.add(exceptionCompany); .set(,list); } } } .info(+ (joinPoint.getTarget().getClass().getName() + + joinPoint.getSignature().getName() + )); }(Exception e) { .error(, e.getMessage()); } } (pointcut=) updateCompany(JoinPoint joinPoint) { {
//擷取方法參數 Object[] args = joinPoint.getArgs(); (args != && args.> && args[].getClass() == Company.) { Company company = (Company) args[]; (.exists()){ Object object = .get(); (object == ){}{ List<ExceptionCompany> list = (List<ExceptionCompany>) object; (i= ;i<list.size();i++){ (list.get(i).getId().equals(company.getId())){ list.get(i).setBusinessLicense(company.getBusinessLicense()); list.get(i).setCompanyName(company.getCompanyName()); list.get(i).setCompanyAddress(company.getCompanyAddress()); list.get(i).setLinkMan(company.getLinkMan()); list.get(i).setLinkTel(company.getLinkTel()); list.get(i).setOrganizationCode(company.getOrganizationCode()); list.get(i).setTaxRegistration(company.getTaxRegistration()); ; } } .set(,list); } } } .info(+ (joinPoint.getTarget().getClass().getName() + + joinPoint.getSignature().getName() + )); } (Exception e) { .error(, e.getMessage()); } } () deleteCompany(JoinPoint joinPoint) { {
//擷取方法參數 Object[] args = joinPoint.getArgs(); (args != && args.> && args[].getClass() == Company.) { Company company = (Company) args[]; (.exists()){ Object object = .get(); (object == ){ }{ List<ExceptionCompany> list = (List<ExceptionCompany>) object; (i= ;i<list.size();i++){ (list.get(i).getId().equals(company.getId())){ list.remove(list.get(i)); ; } } .set(,list); } } } }(Exception e) { } }}2、在application-*.xml檔案中加入如下配置: <!--使用<aop:aspectj-autoproxy/>來開啟註解風格的@AspectJ支援--> <!--Spring AOP的配置 使用CGLib動態代理技術織入增強--> <aop:aspectj-autoproxy/> <!--切面--> <bean id="receiveCache" class="com.aspect.demo.ReceiveCacheManager"/>
SpringAOP實現redis緩衝和mysql資料庫同步