标签: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"/>
标签:spring Aop @AfterReturning MySQL和Redis 数据同步
原文地址:http://blog.51cto.com/11364212/2087622