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"/>