五种通知注解
@Before:前置通知,在方法执行之前执行
@After:后置通知,在方法执行之后执
@AfterRunning:返回通知,在方法返回结果之后执行
@AfterThrowing:异常通知,在方法抛出异常之后执行
@Around:环绕通知,在整个方法执行过程中执行,围绕着方法执行。
五种注解的位置
try {
//前置通知
method.invoke(target,args);//方法执行
//返回通知
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
//异常通知
}finally {
//后置通知
}
//整个环绕通知在整个执行阶段任意期间都可以通知
使用方法
1 .在Spring中启用AspectJ注解支持, 导入jar包:
- com.springsource.net.sf.cglib-2.2.0.jar
- com.springsource.org.aopalliance-1.0.0.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- spring-aop-4.0.0.RELEASE.jar
- spring-aspects-4.0.0.RELEASE.jar
2.配置文件:
打开命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.aspectj.test"></context:component-scan>
<aop:aspectj-autoproxy/>
</beans>
被通知的类
package com.aspectj.test;
import org.springframework.stereotype.Component;
@Component
public class MathTest {
public int div(int i,int j) {
return i/j;
}
}
切入点表达式
切入点表达式需写在所有通知方法前,用来定位一个或多个具体的连接点
execution([权限修饰符] [返回值类型] [简单类名/全类名] [方法名]([参数列表]))
“*”表示任意值
@Befor前置通知
@Component
@Aspect
public class MyLogger {
@Before(value="execution(public int com.aspectj.test.MathTest.div(int, int))")
public void beforLogger(JoinPoint joinPoint) {
System.out.println("method:" + joinPoint.getSignature().getName() + "\targs:" + Arrays.toString(joinPoint.getArgs()));
}
}
输出结果:
method:div args:[6, 2]
@After:后置通知
@Component
@Aspect
public class MyLogger {
@After(value="execution(public int com.aspectj.test.MathTest.div(int, int))")
public void beforLogger(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName() + "方法执行后");
}
}
输出结果:
div方法执行后
@AfterRunning:返回通知
@Component
@Aspect
public class MyLogger {
@AfterReturning(value="execution(public int com.aspectj.test.MathTest.div(int, int))",returning="result")
public void beforLogger(JoinPoint joinPoint,Object result) {
System.out.println(joinPoint.getSignature().getName() + "方法执行后\t结果为:" + result);
}
}
输出结果:
div方法执行后 结果为:3
@AfterThrowing:异常通知
@Component
@Aspect
public class MyLogger {
@AfterThrowing(value="execution(public int com.aspectj.test.MathTest.div(int, int))",throwing="e")
public void beforLogger(JoinPoint joinPoint,Exception e) {
System.out.println("出现了异常!异常为:" + e);
}
}
输出结果:
出现了异常!异常为:java.lang.ArithmeticException: / by zero
@Around:环绕通知
@Component
@Aspect
@Order
public class MyLogger {
@Around(value="execution(public int com.aspectj.test.MathTest.div(int, int))")
public Object beforLogger(ProceedingJoinPoint ProceedingJoinPoint) {
try {
System.out.println("方法执行前");//此处相当于前置通知
Object proceed = ProceedingJoinPoint.proceed();
System.out.println("输出结果:" + proceed);
System.out.println("方法执行后");//此处相当于返回通知
return proceed;
} catch (Throwable e) {
System.out.println("出现异常!");//此处相当于异常通知
e.printStackTrace();
} finally {
System.out.println("finally中!");//此处相当于后置通知
}
return null;
}
}
输出结果:
方法执行前
输出结果:3
方法执行后
finally中!
因为没有异常,所以看不到异常通知。当然也可以自己弄个异常:)