springboot AOP 切面编程测试

 

Pointcut                     切点
Before                       进入切点方法前执行
afterThrowing                方法返回之后执行
AfterReturning               方法抛出异常时执行
package com.modules.aop;

import java.lang.annotation.*;

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemControllerLog {
    String LogAction() default "";

    String LogContent() default "";

    int ModuleID() default 0;
}

package com.modules.aop;

public class SystemLogModel {
    private String LogAction;
    private String LogContent;
    private String FlagID;
    private String FlagName;
    private String LogIP;
    private String TimeFlag;
    private int ModuleID;
}


package com.modules.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 切点类
 */
@Aspect
@Component
public class SystemLogAspect {

    // 本地异常日志记录对象
    protected static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

//    @Autowired
//    private SystemLogMapper systemLogMapper;

    // Controller层切点,针对在业务模块标注SystemControllerLog注解记录日志
    @Pointcut("@annotation(com.modules.aop.SystemControllerLog )")
    public void controllerAspect() {
    }

    // 切点 com.modules.common目录下方法
    @Pointcut("execution(public * com.modules.common.service.impl..*(..))")
    public void around() {
    }

    /**
     * 前置通知 用于拦截Controller层记录用户的操作
     *
     * @param joinPoint 切点
     */
    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        try {
            // 请求的IP
            String logIP = request.getHeader("X-Real-IP");
//       if (StringUtils.isEmpty(logIP)) {
//          logIP = request.getRemoteAddr();
//       }
            String userID = request.getParameter("UserID");
            String userName = request.getParameter("UserName");
//       if (StringUtils.isEmpty(userID) || StringUtils.isEmpty(userName)) {
//          logger.debug("操作日志-->日志添加:用户名或用户ID为空,返回不添加日志!");
//          return;
//       }

            SystemLogModel slm = getControllerMethodDescription(joinPoint);
            slm.setLogIP(logIP);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
            String date = dateFormat.format(new Date());
            slm.setTimeFlag(date);
            slm.setFlagID(userID);
            slm.setFlagName(userName);

            // *========控制台输出=========*//
            logger.debug("=====注解参数获取开始=====");
            logger.debug("请求方法:"
                    + (joinPoint.getTarget().getClass().getName() + "."
                    + joinPoint.getSignature().getName() + "()"));
            logger.debug("操作模块:" + slm.getModuleID());
            logger.debug("操作方法:" + slm.getLogAction());
            logger.debug("操作内容:" + slm.getLogContent());
            logger.debug("请求IP:" + slm.getLogIP());
            logger.debug("FlagID:" + slm.getFlagID());
            logger.debug("FlagName:" + slm.getFlagName());
            // *========数据库日志=========*//

            logger.info(">>>>>>>>保存日志成功");
        } catch (Exception e) {
            // 记录本地异常日志
            logger.error("前置通知异常,保存日志异常信息:{}", e.getMessage());
        }
    }

    @AfterReturning(pointcut = "around()", returning = "obj")
    public void afterAdvice(Object obj) {
        logger.info("controller return msg" + JSON.toJSONString(obj));
    }

    @AfterThrowing(pointcut = "controllerAspect()", throwing = "t")
    public void afterThrowing(JoinPoint joinPoint, Throwable t) {
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        logger.info("method name:" + signature.getName());
        logger.info("参数:" + args.toString());
        logger.error("error message:" + t.getMessage());
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param joinPoint 切点
     * @return 方法描述
     * @throws Exception
     */
    public static SystemLogModel getControllerMethodDescription(
            JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String description = "";
        SystemControllerLog log;
        SystemLogModel logM = new SystemLogModel();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    log = method.getAnnotation(SystemControllerLog.class);
                    logM.setModuleID(log.ModuleID());
                    logM.setLogAction(log.LogAction());
                    logM.setLogContent(log.LogContent());
                    break;
                }
            }
        }
        return logM;
    }
}

package com.modules.common.service.impl;
public class CommonServiceImpl implements ICommonService {
    @Override
    @DataSource(nodeName = "mainNode")
    @SystemControllerLog(LogAction = "查询", ModuleID = 12, LogContent = "测试aop示例")
    public String test(String period) {
return "Y"
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值