package com.zhujie;
import java.lang.management.GarbageCollectorMXBean;
import org.aopalliance.intercept.Invocation;
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.springframework.stereotype.Component;
0
@Component("jj") //注解
@Aspect //声明通知类
public class zhujietongzhi {
@Before("execution(public * addstudent(..))")
public void mybefore(JoinPoint jp) {
System.out.println("注解形式前置通知");
System.out.println(jp.getTarget()+"\n"+jp.getArgs().length+"\n");
}
@AfterReturning(pointcut="execution(public * addstudent(..))",returning="returnvalue")
public void myafter(JoinPoint jp ,Object returnvalue/*方法没有返回值则不用*/) {
System.out.println("注解形式后置通知");
System.out.print(jp.getThis());
}
@AfterThrowing(pointcut="execution(public * addstudent(..))",throwing="e")
public void myexception(JoinPoint jp ,IndexOutOfBoundsException e) {
System.out.println("注解形式的异常通知");
System.out.println(e.getMessage());
}
@Around("execution(public * addstudent(..))")
public void myaround(ProceedingJoinPoint pjp) {
try {
System.out.println("beforex");
pjp.proceed();
System.out.println("endx");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error1x");
} catch (Throwable e) {
// TODO Auto-generated catch block
System.out.println("error1x");
}finally {
System.out.println("end-advice");
}
}
}