Spring AOP

 

<!-- 启动对@AspectJ注解的支持 -->
		 <aop:aspectj-autoproxy proxy-target-class="true" />  

 

 

package yingjun.aop;

import java.util.Arrays;
import java.util.UUID;

import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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;

@Component("aop")
@Aspect
public class AopMethod {

@Pointcut("execution( * yingjun.action.*.add*())")
public void myMethod(){}

/*@Before("myMethod()")
public void beforeMethod(){
	System.out.println("before method...AOP!!!!");
}*/
@AfterReturning(pointcut="myMethod()",returning="value")
public void AfterReturningMethod(JoinPoint joinPoint, Object value){
	System.out.println(joinPoint.getSignature().getName());//获取方法名
	System.out.println(Arrays.toString(joinPoint.getArgs()));//获取参数
	System.out.println(joinPoint.getTarget());//获取获取连接点所在的目标对象
	System.out.println(joinPoint.getThis());//获取代理对象本身
	System.out.println(value); //获取方法的返回值
	System.out.println("正常返回");
}
@AfterThrowing("myMethod()")
public void AfterThrowing(){
	System.out.println("After Throwing...AOP!!!!");
}
@Around("myMethod()")
public void Around(ProceedingJoinPoint pjp) throws Throwable{
	StopWatch clock = new StopWatch();
	clock.start();
	pjp.proceed();
	clock.stop();
	System.out.println("该方法消耗时间"+clock.getTime()+"ms");
}

}



 

你可能感兴趣的:(spring,AOP)