测试 后置通知在哪个语句里面执行
@After:后置通知,在目标对象方法的finally字句中执行
- package com.atguigu.spring.aop.annotation;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.stereotype.Component;
-
- import java.util.Arrays;
-
- /**
- * Date:2022/7/4
- * Author:ybc
- * Description:
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- @After("pointCut()")
- public void afterAdviceMethod(){
- System.out.println("LoggerAspect,后置通知");
-
- }
- }

测试
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(1, 0);
- }
- }


添加连接点
-
- /**
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
-
- }
- }


测试有没有输出
-
- /**
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- @AfterReturning:返回通知,在目标对象方法返回值之后执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //前置通知
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- //后置通知
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
- }
-
-
- //返回通知
- @AfterReturning("pointCut()")
- public void afterReturningAdviceMethod(){
- System.out.println("LoggerAspect,返回通知");
- }
- }

测试:
-
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(1, 0);
- }
- }


并没有输出返回通知 因为方法执行过程出现了异常 有了异常 所以没有返回值
返回通知是在有返回的时候执行的 因为有了异常 没有返回值 所以执行返回通知
我们把发生异常的地方修改下 修改后如下
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(1, 1);
- }
- }


- /**
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- * @AfterReturning:返回通知,在目标对象方法返回值之后执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //前置通知
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- //后置通知
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
- }
-
- //返回通知
- /**
- * 在返回通知中若要获取目标对象方法的返回值
- * 只需要通过@AfterReturning注解的returning属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法的返回值的参数
- */
- @AfterReturning(value = "pointCut()", returning = "result")
- public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
- // System.out.println("LoggerAspect,返回通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
- }
-
- }


- /*
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- * @AfterReturning:返回通知,在目标对象方法返回值之后执行
- * @AfterThrowing:异常通知,在目标对象方法的catch字句中执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //前置通知
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- //后置通知
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
- }
-
- //返回通知
- /**
- * 在返回通知中若要获取目标对象方法的返回值
- * 只需要通过@AfterReturning注解的returning属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法的返回值的参数
- */
- @AfterReturning(value = "pointCut()", returning = "result")
- public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
- // System.out.println("LoggerAspect,返回通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
- }
-
-
- //异常通知
- @AfterThrowing( "pointCut()")
- public void afterThrowingAdviceMethod(JoinPoint joinPoint){
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",异常通知");
- }
-
-
-
- }

测试:
-
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(1, 0);
- }
- }


获取异常
- /**
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- * @AfterReturning:返回通知,在目标对象方法返回值之后执行
- * @AfterThrowing:异常通知,在目标对象方法的catch字句中执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //前置通知
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- //后置通知
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
- }
-
- //返回通知
- /**
- * 在返回通知中若要获取目标对象方法的返回值
- * 只需要通过@AfterReturning注解的returning属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法的返回值的参数
- */
- @AfterReturning(value = "pointCut()", returning = "result")
- public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
- // System.out.println("LoggerAspect,返回通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
- }
-
- //异常通知
- /* @AfterThrowing( "pointCut()")
- public void afterThrowingAdviceMethod(JoinPoint joinPoint){
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",异常通知");
- }*/
-
- /**
- * 在异常通知中若要获取目标对象方法的异常
- * 只需要通过AfterThrowing注解的throwing属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法出现的异常的参数
- */
- @AfterThrowing(value = "pointCut()", throwing = "ex")
- public void afterThrowingAdviceMethod(JoinPoint joinPoint, Throwable ex){
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",异常:"+ex);
- }
- }


- /**
- * 1、在切面中,需要通过指定的注解将方法标识为通知方法
- * @Before:前置通知,在目标对象方法执行之前执行
- * @After:后置通知,在目标对象方法的finally字句中执行
- * @AfterReturning:返回通知,在目标对象方法返回值之后执行
- * @AfterThrowing:异常通知,在目标对象方法的catch字句中执行
- *
- * 2、切入点表达式:设置在标识通知的注解的value属性中
- * execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
- * execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
- * 第一个*表示任意的访问修饰符和返回值类型
- * 第二个*表示类中任意的方法
- * ..表示任意的参数列表
- * 类的地方也可以使用*,表示包下所有的类
- * 3、重用切入点表达式
- * //@Pointcut声明一个公共的切入点表达式
- * @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- * public void pointCut(){}
- * 使用方式:@Before("pointCut()")
- *
- * 4、获取连接点的信息
- * 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
- * //获取连接点所对应方法的签名信息
- * Signature signature = joinPoint.getSignature();
- * //获取连接点所对应方法的参数
- * Object[] args = joinPoint.getArgs();
- *
- */
-
- @Component
- @Aspect //将当前组件标识为切面
- public class LoggerAspect {
-
- @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- public void pointCut(){}
-
- //前置通知
- //@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("pointCut()")
- public void beforeAdviceMethod(JoinPoint joinPoint) {
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- //获取连接点所对应方法的参数
- Object[] args = joinPoint.getArgs();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
- }
-
- //后置通知
- @After("pointCut()")
- public void afterAdviceMethod(JoinPoint joinPoint){
- // System.out.println("LoggerAspect,后置通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",执行完毕");
- }
-
- //返回通知
- /**
- * 在返回通知中若要获取目标对象方法的返回值
- * 只需要通过@AfterReturning注解的returning属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法的返回值的参数
- */
- @AfterReturning(value = "pointCut()", returning = "result")
- public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
- // System.out.println("LoggerAspect,返回通知");
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",结果:"+result);
- }
-
- //异常通知
- /* @AfterThrowing( "pointCut()")
- public void afterThrowingAdviceMethod(JoinPoint joinPoint){
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",异常通知");
- }*/
-
- /**
- * 在异常通知中若要获取目标对象方法的异常
- * 只需要通过AfterThrowing注解的throwing属性
- * 就可以将通知方法的某个参数指定为接收目标对象方法出现的异常的参数
- */
- @AfterThrowing(value = "pointCut()", throwing = "ex")
- public void afterThrowingAdviceMethod(JoinPoint joinPoint, Throwable ex){
- //获取连接点所对应方法的签名信息
- Signature signature = joinPoint.getSignature();
- System.out.println("LoggerAspect,方法:"+signature.getName()+",异常:"+ex);
- }
-
-
- //环绕通知
- @Around("pointCut()")
- //环绕通知的方法的返回值一定要和目标对象方法的返回值一致
- public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
- Object result = null;
- try {
- System.out.println("环绕通知-->前置通知");
- //表示目标对象方法的执行
- result = joinPoint.proceed();
- System.out.println("环绕通知-->返回通知");
- } catch (Throwable throwable) {
- throwable.printStackTrace();
- System.out.println("环绕通知-->异常通知");
- } finally {
- System.out.println("环绕通知-->后置通知");
- }
- return result;
- }
-
-
-
-
- }

测试:
- /**
- public class AOPByAnnotationTest {
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(10, 1);
- }
- }


创建验证功能切面 ValidateAspect
-
- @Component
- @Aspect
- public class ValidateAspect {
-
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("com.atguigu.spring.aop.annotation.LoggerAspect.pointCut()")
- public void beforeMethod(){
- System.out.println("ValidateAspect-->前置通知");
- }
-
- }
测试
- package com.atguigu.spring.test;
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(10, 1);
- }
- }

设置优先级@Order(1)
- @Component
- @Aspect
- @Order(1)
- public class ValidateAspect {
-
- //@Before("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
- @Before("com.atguigu.spring.aop.annotation.LoggerAspect.pointCut()")
- public void beforeMethod(){
- System.out.println("ValidateAspect-->前置通知");
- }
-
- }
测试;
- public class AOPByAnnotationTest {
-
- @Test
- public void testAOPByAnnotation(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
- //获取目标对象
- // CalculatorImpl calculator = ioc.getBean(CalculatorImpl.class);
- //获取代理对象
- Calculator calculator = ioc.getBean(Calculator.class);
- calculator.div(10, 1);
- }
- }

总结:
切面的优先级 * 可以通过@Order注解的value属性设置优先级,默认值Integer的最大值 * @Order注解的value属性值越小,优先级越高
全部总结:
/*
* 1、在切面中,需要通过指定的注解将方法标识为通知方法
* @Before:前置通知,在目标对象方法执行之前执行
* @After:后置通知,在目标对象方法的finally字句中执行
* @AfterReturning:返回通知,在目标对象方法返回值之后执行
* @AfterThrowing:异常通知,在目标对象方法的catch字句中执行
*
*
* 2、切入点表达式:设置在标识通知的注解的value属性中
* execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int)
* execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..)
* 第一个*表示任意的访问修饰符和返回值类型
* 第二个*表示类中任意的方法
* ..表示任意的参数列表
* 类的地方也可以使用*,表示包下所有的类
* 3、重用切入点表达式
* //@Pointcut声明一个公共的切入点表达式
* @Pointcut("execution(* com.atguigu.spring.aop.annotation.CalculatorImpl.*(..))")
* public void pointCut(){}
* 使用方式:@Before("pointCut()")
*
* 4、获取连接点的信息
* 在通知方法的参数位置,设置JoinPoint类型的参数,就可以获取连接点所对应方法的信息
* //获取连接点所对应方法的签名信息
* Signature signature = joinPoint.getSignature();
* //获取连接点所对应方法的参数
* Object[] args = joinPoint.getArgs();
*
* 5、切面的优先级
* 可以通过@Order注解的value属性设置优先级,默认值Integer的最大值
* @Order注解的value属性值越小,优先级越高
*
*/