Spring面向切面编程AOP(around)

详细分析Spring AOP实现的JDK代理AOP,与CGLIB实现AOP代理; http://blog.csdn.net/u013126379/article/details/52121096


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;
	}
	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值