spring-AOP-添加日志

1 把一个类声明为一个切面:①需要把该类放入到IOC中,②再声明为一个切面(@Aspect @Component)@Order(1):指定顺序

2 在配置文件中添加如下配置:<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

package com.atguigu.aop;

import java.util.Arrays;

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.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/*
 * 把一个类声明为一个切面:1,需要把该类放入到IOC容器中;2,再声明为切面
 * 
 */
@Aspect
@Component
public class LoggingAspect {

    /*定义一个方法,用于声明切入点表达式,一般地,该方法不需要添加其他的代码
     * 使用@PointCut来声明切入点表达式
     * 后面的其他通知直接使用方法名来引用当前的切入点表达式
     * 
     */
    @Pointcut("execution(* com.atguigu.aop.*.*(..))")
    public void declareJoinPointExpression(){}
    
    @Before("declareJoinPointExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object [] args = joinPoint.getArgs();
        
        System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
    }
    //无论是否有异常都会执行
    @After("declareJoinPointExpression()")
    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " end " );
    }
    
    @AfterReturning(value="declareJoinPointExpression()",returning="result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }
    
    @AfterThrowing(value="declareJoinPointExpression()",throwing="ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs with " + ex);
    }
    /*
     *     环绕通知需要传递一个ProceedingJoinPoint参数
     *     环绕通知类似于动态代理的全过程;ProceedingJoinPoint类型的参数可以决定是否执行目标方法
     *     环绕通知必须要有返回值,该返回值即使目标方法的返回值
     */
//    @Around("execution(* com.atguigu.aop.*.*(..))")
//    public Object around(ProceedingJoinPoint point){
//        Object result = null;
//        String methodName = point.getSignature().getName();
//        try {
//            //前置通知
//            System.out.println("The method "+methodName+" start with "+Arrays.asList(point.getArgs()));
//            result = point.proceed();
//            //返回通知
//            System.out.println("The method "+methodName+" end with "+result);
//        } catch (Throwable e) {
//            //异常通知
//            System.out.println("The method "+methodName+" occurs with "+e);
//            e.printStackTrace();
//        }
//        //后置通知
//        System.out.println("The method "+methodName+" end");
//        return result;
//    }
}

 

posted on 2015-12-09 12:29  james-roger  阅读(269)  评论(0编辑  收藏  举报