AOP注解日志

AOP日志

1,导入Aspect的依赖包
		<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>		
2,因为都是基于springboot的项目,所以不再xml里面配置aop,此时需要些aop自定义注解
	//作用于方法
	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface LogAnnotation{
      		String operationName();
      		String operationType();
	}
3,需要在哪个方法上面开启AOP日志,就打上自定义的LogAnnotation注解,如:
//便于在具体处理日志的时候拿出来
 @LogAnnotation(operationType = "/login",operationName = "用户登录")
        @RequestMapping("/login")
        public String login(String username,String password){
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isAuthenticated()) {
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
                subject.login(token);
            }
            return "success";
        }
4,在具体的类里面开启AOP日志
public class LogAspect {

    @Pointcut("execution(* persion.david..controller.*.*(..))")
    public void pointCut() {

    }

   // @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws Exception {
        //我想要先知道用户在做什么
        //获取正在被拦截的方法
        String name = joinPoint.getTarget().getClass().getName();
        String name1 = joinPoint.getSignature().getName();
        // System.out.println(joinPoint.getSignature().toString());
        Object[] args = joinPoint.getArgs();
        //System.out.println("name:====>" + name + "---------->name1:--->" + name1 + "=-=-=-=-=-=args:==>" + Arrays.toString(args));
        //找方法上面的注解
        Class<?> clazz = Class.forName(name);


        // Method method = clazz.getMethod(name1,);
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            // System.out.println(method.toString());
            if (method.getName().equalsIgnoreCase(name1)) {//当前遍历到的方法和正在使用的方法是一样的

                LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
                //获取注解的值
                if (logAnnotation != null) {
                    String operationName = logAnnotation.operationName();
                    String operationType = logAnnotation.operationType();
                    //System.out.println("operationName:===?" + operationName + "     --->operationType:==>" + operationType);
                    SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
                    System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName()));
                }
            }


        }

    }

    /**
     * 约定优于编码,这个时候我们发现我们的controller因为返回值类型五花八门,导致我们在这里很难判断到底是成功还是失败,所以我们应该将返回值约定为统一类型, 比如resultbean类型,然后根据我们在里面约定的错误码,来决定显示成功还是失败
     * @param joinPoint
     * @param o
     * @throws Exception
     */
    @AfterReturning(value = "pointCut()", returning = "o")
    public void after(JoinPoint joinPoint, Object o) throws Exception {
        //我想要先知道用户在做什么
        //获取正在被拦截的方法
        String name = joinPoint.getTarget().getClass().getName();
        String name1 = joinPoint.getSignature().getName();
        // System.out.println(joinPoint.getSignature().toString());
        Object[] args = joinPoint.getArgs();
        //System.out.println("name:====>" + name + "---------->name1:--->" + name1 + "=-=-=-=-=-=args:==>" + Arrays.toString(args));
        //找方法上面的注解
        Class<?> clazz = Class.forName(name);


        // Method method = clazz.getMethod(name1,);
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            // System.out.println(method.toString());
            if (method.getName().equalsIgnoreCase(name1)) {//当前遍历到的方法和正在使用的方法是一样的

                LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
                //获取注解的值
                if (logAnnotation != null) {
                    String operationName = logAnnotation.operationName();
                    String operationType = logAnnotation.operationType();
                    //System.out.println("operationName:===?" + operationName + "     --->operationType:==>" + operationType);
                    SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
                    System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName()));
                }
            }

        }
    }

    @AfterThrowing(pointcut = "pointCut()",throwing = "t")
    public void  afterThrowing(JoinPoint joinPoint,Throwable t) throws Exception {
        //我想要先知道用户在做什么
        //获取正在被拦截的方法
        String name = joinPoint.getTarget().getClass().getName();
        String name1 = joinPoint.getSignature().getName();
        // System.out.println(joinPoint.getSignature().toString());
        Object[] args = joinPoint.getArgs();
        //System.out.println("name:====>" + name + "---------->name1:--->" + name1 + "=-=-=-=-=-=args:==>" + Arrays.toString(args));
        //找方法上面的注解
        Class<?> clazz = Class.forName(name);


        // Method method = clazz.getMethod(name1,);
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            // System.out.println(method.toString());
            if (method.getName().equalsIgnoreCase(name1)) {//当前遍历到的方法和正在使用的方法是一样的

                LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
                //获取注解的值
                if (logAnnotation != null) {
                    String operationName = logAnnotation.operationName();
                    String operationType = logAnnotation.operationType();
                    //System.out.println("operationName:===?" + operationName + "     --->operationType:==>" + operationType);
                    SysAdmin sysAdmin = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
                    System.out.println(operationName + "被执行了(" + operationType + "),参数是:" + Arrays.toString(args) + (sysAdmin == null ? "" : "执行的用户是:==>" + sysAdmin.getName())+"结果失败了");
                    //在这里可以实现自己的业务逻辑,进行日志操作入库

                }
            }

        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值