Spring面向切面编程AOP(around)
2017-05-26 16:28
686 查看
详细分析Spring AOP实现的JDK代理AOP,与CGLIB实现AOP代理; http://blog.csdn.net/u013126379/article/details/52121096
1、采用注释注入的方式
配置文件:
表示使用CGLib动态代理技术织入增强
代码编写:
2、xml配置方式
表示使用jdk动态代理织入增强
配置文件:
代码编写:
1、采用注释注入的方式
配置文件:
<aop:aspectj-autoproxy proxy-target-class="true"/>
表示使用CGLib动态代理技术织入增强
代码编写:
package com.ldygo.order.mapper.proxy; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ldygo.order.dao.mybatis.client.OrderMasterMapper; import com.ldygo.order.dao.mybatis.dto.OrderMaster; import com.ldygo.order.mongo.dao.OrderLogMongoDBService; import com.ldygo.order.mongo.dto.OrderLogMongoDTO; /** * <p> Title: OrderLogAOPServiceImpl </p> * <p> Description: 订单日志 </p> * <p> Copyright: openlo.cn Copyright (C) 2017 </p> * * @author huangl * @since 2017年5月26日 上午10:08:45 */ @Component("orderLogAOPServiceImpl") @Aspect public class OrderLogAOPServiceImpl implements IAOPService{ @Autowired private OrderLogMongoDBService orderLogMongoDBService; @Autowired private OrderMasterMapper orderMasterMapper; @Around("execution(* com.ldygo.order.dao.mybatis.client.OrderMasterMapper.updateByPrimaryKeySelective(..))") public Object aroundAdvice(ProceedingJoinPoint call){ Object object = null; try { object = call.proceed(); } catch (Throwable e) { e.printStackTrace(); } finally { //1.判断是否修改成功 if(object !=null && object instanceof Integer){ int i = (int) object; if(i>0){ //2.如果修改成功,获取参数 Object[] argums = call.getArgs(); if(argums!=null && argums.length>0){ OrderMaster order = (OrderMaster) argums[0]; String orderNO = order.getOrderNo(); OrderMaster orderMaster = this.orderMasterMapper.selectByPrimaryKey(orderNO); //3.设置订单日志参数 OrderLogMongoDTO orderLogMongoDTO = new OrderLogMongoDTO(); orderLogMongoDTO.setOrderNo(orderMaster.getOrderNo()); orderLogMongoDTO.setUpdateBy(orderMaster.getUpdatedBy()); orderLogMongoDTO.setOrderMaster(orderMaster); try { this.orderLogMongoDBService.insertOrderLog(orderLogMongoDTO); } catch (Exception e) { e.printStackTrace(); } } } } } return object; } }
2、xml配置方式
表示使用jdk动态代理织入增强
配置文件:
<bean id="xmlHandler" class="com.ldygo.order.proxy.OrderLogAOPServiceImpl" /> <aop:config> <aop:aspect id="aspect" ref="xmlHandler"> <aop:pointcut id="pointUserMgr" expression="execution(* com.ldygo.order.dao.mybatis.client.OrderMasterMapper.updateByPrimaryKeySelective(..))"/> <aop:around method="aroundAdvice" pointcut-ref="pointUserMgr"/> </aop:aspect> </aop:config>
代码编写:
package com.ldygo.order.proxy; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.beans.factory.annotation.Autowired; import com.ldygo.order.dao.mybatis.client.OrderMasterMapper; import com.ldygo.order.dao.mybatis.dto.OrderMaster; import com.ldygo.order.mongo.dao.OrderLogMongoDBService; import com.ldygo.order.mongo.dto.OrderLogMongoDTO; /** * <p> Title: OrderLogAOPServiceImpl </p> * <p> Description: 订单日志 </p> * <p> Copyright: openlo.cn Copyright (C) 2017 </p> * * @author huangl * @since 2017年5月26日 上午10:08:45 */ //@Component("orderLogAOPServiceImpl") //@Aspect public class OrderLogAOPServiceImpl { @Autowired private OrderLogMongoDBService orderLogMongoDBService; @Autowired private OrderMasterMapper orderMasterMapper; //@Around("execution(* com.ldygo.order.dao.mybatis.client.OrderMasterMapper.updateByPrimaryKeySelective(..))") public Object aroundAdvice(ProceedingJoinPoint call){ //org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory; Object object = null; try { object = call.proceed(); } catch (Throwable e) { e.printStackTrace(); } finally { //1.判断是否修改成功 if(object !=null && object instanceof Integer){ int i = (int) object; if(i>0){ //2.如果修改成功,获取参数 Object[] argums = call.getArgs(); if(argums!=null && argums.length>0){ OrderMaster order = (OrderMaster) argums[0]; String orderNO = order.getOrderNo(); OrderMaster orderMaster = this.orderMasterMapper.selectByPrimaryKey(orderNO); //3.设置订单日志参数 OrderLogMongoDTO orderLogMongoDTO = new OrderLogMongoDTO(); orderLogMongoDTO.setOrderNo(orderMaster.getOrderNo()); orderLogMongoDTO.setUpdateBy(orderMaster.getUpdatedBy()); orderLogMongoDTO.setOrderMaster(orderMaster); try { this.orderLogMongoDBService.insertOrderLog(orderLogMongoDTO); } catch (Exception e) { e.printStackTrace(); } } } } } return object; } }
相关文章推荐
- Spring AOP 面向切面编程相关注解
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- 使用Spring进行面向切面(AOP)编程
- [Spring]面向切面编程AOP【学习笔记】
- Spring 面向切面编程AOP
- Spring面向切面编程AOP
- Spring笔记——使用Spring进行面向切面(AOP)编程
- 深入分析JavaWeb 53 -- Spring中的AOP面向切面编程1
- 从头认识Spring-1.2 什么是AOP?为什么需要面向切面编程?
- Spring面向切面编程AOP的个人理解
- Spring in Action 入门之面向切面编程AOP
- spring AOP面向切面编程的四种实现方式
- spring面向切面编程AOP(Aspect-orented programming)
- Spring笔记——使用Spring进行面向切面(AOP)编程
- Spring in Action 入门之面向切面编程AOP
- 使用Spring进行面向切面(AOP)编程
- Spring面向切面编程AOP
- 【spring-boot】spring aop 面向切面编程初接触
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-使用工厂创建代理(Using the ProxyFactoryObject to create AOP proxies)
- 【spring-boot】spring aop 面向切面编程初接触