当前位置 : 主页 > 编程语言 > java >

spring aop注解

来源:互联网 收集:自由互联 发布时间:2021-06-28
切面类LoggingAespect package com.aoe.aop;import java.util.Arrays;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lan
切面类LoggingAespect
package com.aoe.aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
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;
import org.springframework.stereotype.Component;

@Aspect  
@Component  
public class LoggingAespect {
	private long start;
	private long end;
	private static Logger logger = Logger.getLogger("logDailyFile");
	@Pointcut("execution(* com.xukun.controller.*.*(..))")
	public void pointCut_(){
	}
	
	@Before(value="pointCut_()")
	public void beforeMethod(JoinPoint joinPoint){  
        String methodName = joinPoint.getSignature().getName();  
        Object args = Arrays.asList(joinPoint.getArgs());  
         start = System.currentTimeMillis();
        logger.info("The method:"+methodName +" begins with "+ args);
    }  
      
    //后置通知:在目标方法之后(无论是否发生异常),执行的通知,  
    //在后置通知中还不能访问目标方法执行的结果。执行结果须在返回通知中访问。  
	@After(value="pointCut_()")
    public void afterMethod(JoinPoint joinPoint){  
        String methodName = joinPoint.getSignature().getName(); 
        end = System.currentTimeMillis();
        logger.info("The method:"+ methodName+" ends"+"执行业务方法共计:" + (end - start) + "毫秒");
    }  
    //返回通知:在目标方法正常结束执行后的通知  
    @AfterReturning(value="pointCut_()",returning="result")  
    public void afterRunningMethod(JoinPoint joinPoint , Object result){  
        String methodName = joinPoint.getSignature().getName();  
        logger.info("The method:"+ methodName+" ends with the Result "+ result);
    }  
    //环绕通知 
    //@Around(value="pointCut_()")
	public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		Object result = null;  
		String  methodName = proceedingJoinPoint.getSignature().getName();  
		Object args = Arrays.asList(proceedingJoinPoint.getArgs());  
        try {
        	//前置通知  
        	logger.info("Arround:The method "+methodName +" begins with "+args);
            long start = System.currentTimeMillis();
            result = proceedingJoinPoint.proceed();
          //后置通知  
            long end = System.currentTimeMillis();
            logger.info("Arround:The method "+ methodName+" ends"+","+"执行业务方法共计:" + (end - start) + "毫秒。");
        } catch(Exception e){  
        	e.printStackTrace();  
        	 //异常通知  
            logger.info("Arround:异常监控 "+ methodName+"occurs exception:"+e);
            throw new RuntimeException(e);  
            //不抛出异常的话,异常就被上面抓住,执行下去,返回result,result值为null,转换为int  
        }  
        //返回通知  
        logger.info("Arround:The method "+ methodName+" ends with the Result "+ result);
        return result;  
    }
	//异常通知
	
	 @AfterThrowing(value="pointCut_()",throwing = "e") 
	  public void throwingAdvice(JoinPoint joinPoint, Exception e) {
	        String methodName = joinPoint.getSignature().getName();
	        logger.info("异常监控:" + methodName + " occurs exception: " + e);
	    }
	
}
sping-mvc注解开启
网友评论