package com.woniuxy.stuLog;

    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.woniuxy.entity.Student;
    import com.woniuxy.entity.StudentLog;
    import com.woniuxy.mapper.ClassesMapper;
    import com.woniuxy.mapper.GradeMapper;
    import com.woniuxy.mapper.StudentMapper;
    import com.woniuxy.service.IStudentLogService;
    import com.woniuxy.util.IpUtil;
    import org.apache.shiro.SecurityUtils;
    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.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import java.lang.reflect.Method;
    import java.lang.reflect.Parameter;
    import java.net.SocketException;
    import java.util.Date;

    @Aspect
    @Component
    public class SysLogAspect {

    1. @Autowired<br /> private IStudentLogService studentLogService;<br /> @Autowired<br /> private StudentMapper studentMapper;<br /> @Autowired<br /> private ClassesMapper classesMapper;<br /> @Autowired<br /> private GradeMapper gradeMapper;
    2. //定义切点,切点表达式指向sysLog注解,在业务方法上标上注解,<br /> //所标注的方法都可以进行记录<br /> @Pointcut("@annotation(com.woniuxy.stuLog.SysLog)")<br /> public void logPointCut(){
    3. }
    4. @Around("logPointCut()")<br /> public Object around(ProceedingJoinPoint pjt) {
    5. //获取方法签名<br /> MethodSignature methodSignature = (MethodSignature) pjt.getSignature();<br /> //获取方法<br /> Method method = methodSignature.getMethod();<br /> //获取方法上的注解<br /> SysLog sysLog = method.getAnnotation(SysLog.class);<br /> //获取操作上描述的属性值<br /> String value = sysLog.value();<br /> //获取参数名称<br /> String[] parameterNames = methodSignature.getParameterNames();<br /> //获取参数值<br /> Object[] args = pjt.getArgs();<br /> // method获取参数信息<br /> Parameter[] parameters = method.getParameters();
    6. //准备记录日志信息<br /> StudentLog logRecord=new StudentLog();<br /> //操作说明<br /> logRecord.setOperationInfo(value);<br /> //得到登录人<br /> Student user = (Student) SecurityUtils._getSubject_().getPrincipal();
    7. logRecord.setStudentName(user.getStudentName());
    8. String studentName = user.getStudentName();
    9. QueryWrapper<Student> wrapper=new QueryWrapper<>();<br /> wrapper.eq("student_name", studentName);<br /> Student student = studentMapper.selectOne(wrapper);
    10. Integer classId = student.getClassId();<br /> String className = classesMapper.selectById(classId).getClassName();<br /> logRecord.setClassName(className);
    11. Integer gradeId = classesMapper.selectById(classId).getGradeId();<br /> String gradeName = gradeMapper.selectById(gradeId).getGradeName();
    12. logRecord.setGradeName(gradeName);
    13. Object proceed=null;<br /> try {<br /> //执行代理方法<br /> proceed = pjt.proceed();<br /> } catch (Throwable e) {<br /> e.printStackTrace();<br /> }<br /> //设置操作日期<br /> logRecord.setCreateTime(new Date());<br /> //得到IP地址<br /> try {<br /> logRecord.setIpAddress(IpUtil._getLocalIp4Address_().toString());<br /> } catch (SocketException e) {<br /> e.printStackTrace();<br /> }<br /> //加入数据库<br /> studentLogService.save(logRecord);
    14. return proceed;
    15. }

    }

    获取返回值的写法:

    @Configuration
    @Aspect
    public class UserInfoAspect {
    @Pointcut(“execution( com..test(*))”)
    public void test() {}

    //在事件通知类型中申明returning即可获取返回值
    @AfterReturning(value = “test()”, returning=”returnValue”)
    public void logMethodCall(JoinPoint jp, Object returnValue) throws Throwable {
    System.out.println(“进入后置增强了!”);
    String name = jp.getSignature().getName();
    System.out.println(name);
    Object[] args = jp.getArgs();
    for (Object arg : args) {
    System.out.println(“参数:” + arg);
    }
    System.out.println(“方法返回值为:” + returnValue);
    }
    }
    ————————————————
    版权声明:本文为CSDN博主「诗人与黑客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_41899174/article/details/90732058