victory的博客

长安一片月,万户捣衣声

0%

spring | AOP(切面、通知、切入点、切面优先级)

AOP(切面、通知、切入点、切面优先级)

示例:
1.项目目录

2.MathI.java(接口)

package com.atguigu.spring.aop;

public interface MathI {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

3.MathImpl.java(实现类)

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;

@Component
public class MathImpl implements MathI{

    @Override
    public int add(int i, int j) {
        int result = i + j;
        return result;
    }
    
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        int result = i * j;
        return result; 
    }

    @Override
    public int div(int i, int j) {
        int result = i / j;
        return result;
    }
}

4.MyLoggerAspect.java(切面–>日志)

package com.atguigu.spring.aop;

import java.util.Arrays;
import org.aopalliance.intercept.Joinpoint;
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.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect//标注当前类为切面
@Order(1)
public class MyLoggerAspect {
    //AspectJ支持的5种类型的通知注解
    
    //重用切入点定义
    @Pointcut(value="execution(* com.atguigu.spring.aop.*.*(..))")
    public void test(){
        
    }
    /**
     * @Before:将方法指定为前置通知
     * 必须设置value,其值为切入点表达式
     * 前置通知:作用于方法执行之前
     */
    //前置通知 
//    @Before(value = "execution(public int com.atguigu.spring.aop.MathImpl.add(int, int))")
//    @Before(value = "execution(public int com.atguigu.spring.aop.MathImpl.*(int,int))")
//    @Before(value = "execution(* com.atguigu.spring.aop.MathImpl.*(int,int))")
//    @Before(value = "execution(* com.atguigu.spring.aop.*.*(int,int))")
//    @Before(value = "execution(* com.atguigu.spring.aop.*.*(..))")
    @Before(value = "test()")
    public void beforeMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();//获取方法的参数
        String methodName = joinPoint.getSignature().getName();//获取方法名
        
        System.out.println("method:" + methodName + ",arguments:" + Arrays.toString(args));
    }
    
    /**
     * @After:将方法标注为后置通知
     * 后置通知:作用于方法的finally语句块,即不管有没有异常都会执行
     */
    //后置通知
    //@After(value = "execution(* com.atguigu.spring.aop.*.*(..))")
    @After(value = "test()")
    public void afterMethod(){
        System.out.println("后置通知");
    }
    
    /**
     * @AfterReturning:将方法标注为返回通知
     * 返回通知:作用于方法执行之后
     * 要通过returning设置接收方法返回值的变量名
     * 要向在方法中使用,必须在方法的形参中设置和变量名相同的参数名的参数
     */
    @AfterReturning(value = "execution(* com.atguigu.spring.aop.*.*(..))",returning="result")
    public void afterReturing(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("method:" + methodName + ",result:" + result);
    }
    
    /**
     * @AfterThrowing:将方法标注为异常通知(例外通知)
     * 异常通知:作用于方法抛出异常时
     * 可通过throwing设置接受方法返回的异常信息
     * 在参数列表中可通过具体的异常类型,来对指定的异常信息进行操作
     */
    @AfterThrowing(value = "execution(* com.atguigu.spring.aop.*.*(..))", throwing="ex")
    public void afterThrowing(Exception ex){
        System.out.println("有异常了,messages"+ex);
    }
    //处理特定异常
//    @AfterThrowing(value = "execution(* com.atguigu.spring.aop.*.*(..))", throwing="ex")
//    public void afterThrowing(NullPointerException ex){
//        System.out.println("有异常了,messages"+ex);
//    }
    
    /**
     * @Around:
     */
    @Around(value = "execution(* com.atguigu.spring.aop.*.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){    
        Object result = null;
        try {
            //前置通知
            System.out.println("前置通知");
            result = joinPoint.proceed();//执行方法
            //返回通知
            System.out.println("返回通知");
            return result;
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //异常通知
            System.out.println("异常通知");
        }finally{
            //后置通知
            System.out.println("后置通知");
        }
        return -1;
    }
}

5.TestHandler.java

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;

@Component
public class TestHandler {
    public void test(){
        System.out.println("测试切入点表达式");
    }
}

6.TestAspect.java(切面–>另一个切面,用来测试切面优先级)

package com.atguigu.spring.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(0)//定义切面作用的优先级,值越小优先级越高,默认值为int的最大值 
public class TestAspect {
    @Before(value="execution(* com.atguigu.spring.aop.*.*(..))")
    public void before(){
        System.out.println("TestAspect====>前置通知");
    }
}

7.Test.java(测试类)

package com.atguigu.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");
        
        MathI math = ac.getBean("mathImpl", MathI.class);
        
        int i = math.div(1, 1);
        System.out.println(i);
        
//        TestHandler bean = ac.getBean("testHandler", TestHandler.class);
//        bean.test();
    }
}