aop前置增强、后置、环绕增强

更新时间:2020-05-09 10:08:19 点击次数:1118次
后置

   @AfterReturning(value="@annotation(com.sdyy.biz.act.maint.aop.annotation.ResIfNull)",returning="result")
     public void AfterReturning(JoinPoint joinPoint , Object result) {
         logger.info("AfterReturning------------start");

         ResIfNull myAnno=  ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(ResIfNull.class);
         if ("obj".equals(myAnno.type())) {
             if (result == null) {
                 throw new SysException("初始化流程失败,请定义服务流程");
             }

         } else if ("list".equals(myAnno.type())) {
             if (result instanceof List) {
                 List res = (List) result;
                 if (res.size() == 0) {
                     throw new SysException("初始化流程失败,请配置参数");
                 }
             }

         }

     }

前置

@Before(value="@annotation(com.sdyy.biz.act.maint.aop.annotation.IfMount)")
public void ifMountCheck(JoinPoint joinPoint) throws Exception {
    Object[] args = joinPoint.getArgs();
    if(dsServiceDetailService.getMountCount(args[0].toString())<=0){
        //说明没有挂接数据
        throw new SysException("请挂接数据");
    }
    //获取方法参数


}

环绕

package com.sdyy.common.aop;

import com.sdyy.common.annotation.OperLog;
import com.sdyy.common.controller.BaseController;
import com.sdyy.common.entity.KafkaMessage;
import com.sdyy.common.entity.Log;
import com.sdyy.common.kafka.KafkaProducer;
import com.sdyy.common.retobj.BaseEntity;
import com.sdyy.common.utils.DateUtils;
import com.sdyy.core.auth.operator.entity.AuthOperator;
import lombok.extern.log4j.Log4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 *
 *    * @className: LogAspect
 *   * @description:日志切面
 *   * @param:
 *   * @return:
 *   * @throws:
 *   * @author: lizz
 *   * @date: 2020/03/15 18:37
 *
 */
@Log4j
@Component
@Aspect
public class LogAspect {

    @Autowired
    private KafkaProducer producer;
    /**
     * @Description: 方法切入点
     * @Author: lizz
     * @Date: 2020/3/15 18:56
     * @Reviser:修改人
     * @ReviseDate:修改时间
     * @Revision:修改内容
     **/
    @Pointcut("execution(* com.sdyy.maint.*.controller.*.*(..))")
    public void pointerCutMethod() {
    }

    /**
     * @Description: 环绕通知
     * @Author: lizz
     * @Date: 2020/3/15 18:56
     * @Reviser:修改人
     * @ReviseDate:修改时间
     * @Revision:修改内容
     **/
    @Around(value = "pointerCutMethod() && @annotation(annotation)")
    public Object doAround(ProceedingJoinPoint joinPoint, OperLog annotation) {
        Log logs = new Log();
        //通过注解获取当前属于那个模块
        logs.setModuleName(annotation.moduleName());
        //通过注解获取当前的操作方式
        logs.setOperation(annotation.option());
        logs.setCreatedate(DateUtils.getCurrentDate());
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        Long beginTime = System.currentTimeMillis();
        if (ra != null) {
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            HttpServletRequest request = sra.getRequest();

            String ip = request.getRemoteHost();
            logs.setUserIp(ip);
            logs.setReqUrl(request.getRequestURI());
            // 从session中获取用户信息
            AuthOperator authOperator = (AuthOperator) request.getSession().getAttribute(BaseController.LOGIN_IN_OPERATOR_SESSION);
            if (authOperator != null) {
                logs.setUserName(authOperator.getUserId());
            }
        }
        try {
            Object object = joinPoint.proceed();
            if (object != null) {
                if (object instanceof BaseEntity) {
                    if(((BaseEntity) object).getStatus()==200){
                        logs.setOperResult("成功");
                    }else {
                        logs.setOperResult(((BaseEntity) object).getMessage());
                    }

                }
            }
            KafkaMessage ms =new KafkaMessage();
            ms.setMsg(logs);
            producer.send("logs", ms);
            return object;
        } catch (Throwable e) {

            logs.setOperResult("失败:" + e.getMessage());
            KafkaMessage ms =new KafkaMessage();
            ms.setMsg(logs);
            producer.send("logs", ms);
            log.error("异常"+e);
            return null;
        }
    }
}


本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!