AOP切面实例

package com.chinasofti.vtcsvc.business.aop;


import java.util.Map;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;


import com.chinasofti.vtcsvc.business.VtmErrorInfoService;
import com.chinasofti.vtcsvc.business.model.VtmErrorInfoDomain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@Component
@Aspect
public class SendEmailAop {


private Logger logger = LoggerFactory.getLogger(getClass());


@Qualifier("vtmErrorInfoServiceImpl")
@Autowired
private VtmErrorInfoService vtmErrorInfoService;


@Pointcut("execution(* com.chinasofti.vtcsvc.business.impl.PRDSendEmailServiceImpl.sendEmail*(..))")
public void anyMethod() {
}


// spring中Before通知,在调用sendEmail方法前切入故障数据数据库保存方法
@Before("anyMethod()") 
public void doBefore(JoinPoint joinpoint) {


Map map = extracted(joinpoint);
Assert.notNull(map, "[Assertion joinpoint] - this argument [map] is required; it must not be null");


VtmErrorInfoDomain vtmErrorInfo = vtmErrorInfoService.save("", map);
if (logger.isDebugEnabled()) {
try {
logger.info("故障数据保存方法执行结果:" + new ObjectMapper().writeValueAsString(vtmErrorInfo));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}


/*
* 获取参数集合
*/
private Map extracted(JoinPoint joinpoint) {
@SuppressWarnings("unchecked")
Map map = (Map) joinpoint.getArgs()[1];
return map;
}


}

你可能感兴趣的:(AOP切面实例)