• aop日志管理(摘)


    复制代码
    <bean id="logAopBean" class="com.demo.common.aop.LogAop"></bean>
        <aop:config>
            <aop:aspect id="logAspect" ref="logAopBean">
                <aop:pointcut expression="execution(* com.demo..*(..))" id="allMethod"/>
                <aop:before method="before" pointcut-ref="allMethod" />
                <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod" />
                <aop:after-returning method="afterReturn" pointcut-ref="allMethod" />
                <aop:after method="after" pointcut-ref="allMethod" />
            </aop:aspect>
        </aop:config>
    复制代码
    复制代码

    2、日志处理类,

    复制代码
    复制代码
    /**
     * LogAop.java
     * 
     * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
     * @author wyl
     * @date 2016-10-18
     */ 
    
    package com.demo.common.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * @author wyl
     * @Description TODO 
     * @date 2016-10-18
     *
     */
    
    public class LogAop {
        public void before(JoinPoint call){
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println("开始执行:"+className+"."+methodName+"()方法...");
        }
        public void afterThrowing(JoinPoint call){
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()方法抛出了异常...");
        }
        public void afterReturn(JoinPoint call){
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()方法正常执行结束...");
        }
        public void after(JoinPoint call){
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");
        }
        /*//用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型  
        public Object doAround(ProceedingJoinPoint call) throws Throwable {  
            Object result = null;  
            this.before(call);//相当于前置通知  
            try {  
                result = call.proceed();  
                this.afterReturn(call); //相当于后置通知  
            } catch (Throwable e) {  
                this.afterThrowing(call); //相当于异常抛出后通知  
                throw e;
            }finally{  
                this.after(call);  //相当于最终通知  
            }  
            return result;  
        }*/
    }
    复制代码
    复制代码

    二、注解方式

    1、配置applicationContext.xml,

    <bean id="logAspectBean" class="com.demo.common.aop.LogAnnotationAspect"></bean>
        <aop:aspectj-autoproxy/>

    2、日志处理类,

    复制代码
    复制代码
    /**
     * LogAnnotationAspect.java
     * 
     * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
     * @author wyl
     * @date 2016-10-18
     */ 
    
    package com.demo.common.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    /**
     * @author wyl
     * @Description TODO 
     * @date 2016-10-18
     *
     */
    
    @Aspect  //定义切面类  
    public class LogAnnotationAspect {  
        @SuppressWarnings("unused")  
        //定义切入点,提供一个方法,这个方法的名字就是改切入点的id  
        @Pointcut("execution(* com.demo..*(..))")  
        private void allMethod(){}  
        //针对指定的切入点表达式选择的切入点应用前置通知  
        @Before("allMethod()")    
        public void before(JoinPoint call) {  
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println("开始执行:"+className+"."+methodName+"()方法...");
        }  
        //访问命名切入点来应用后置通知  
        @AfterReturning("allMethod()")  
        public void afterReturn(JoinPoint call) {  
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()方法正常执行结束...");
        }  
        //应用最终通知  
        @After("allMethod()")  
        public void after(JoinPoint call) {  
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");
        }  
        //应用异常抛出后通知  
        @AfterThrowing("allMethod()")  
        public void afterThrowing(JoinPoint call) {  
            String className = call.getTarget().getClass().getName();
            String methodName = call.getSignature().getName();
            System.out.println(className+"."+methodName+"()方法抛出了异常...");
        }  
        //应用周围通知  
        //@Around("allMethod()")  
        public Object doAround(ProceedingJoinPoint call) throws Throwable{  
            Object result = null;  
            this.before(call);//相当于前置通知  
            try {  
                result = call.proceed();  
                this.afterReturn(call); //相当于后置通知  
            } catch (Throwable e) {  
                this.afterThrowing(call);  //相当于异常抛出后通知  
                throw e;  
            }finally{  
                this.after(call);  //相当于最终通知  
            }  
            return result;  
        }  
    }
    复制代码
  • 相关阅读:
    Spring 学习——Spring IOC概念
    Oracle使用——Oracle表空间处理
    Spring 学习——Spring框架结构、概念
    Java错误——The hierarchy of the type is inconsistent错误
    Git教程
    git 常用操作
    "use strict"; 的正确使用
    react学习笔记(二)React表单详解
    chrome 添加有用扩展程序
    react学习笔记(一)
  • 原文地址:https://www.cnblogs.com/Cruyse/p/7449125.html
Copyright © 2020-2023  润新知