AOP五种执行时机

动态代理四种增强方式

先创建一个service类

package com.zzj.calculatar.service;
import org.springframework.stereotype.Service;

@Service
public class CalculatorService implements ICalculatorService{

    @Override
    public int mul(int a, int b) {
        return a*b;
    }

    @Override
    public int div(int a, int b) {
        return a/b;
    }
    
}

XML文件

<context:component-scan base-package="com.zzj"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

四种增强:前置增强,后置增强,返回增强,异常增强

@Order(1)//切片执行顺序,默认为字典顺序
@Aspect
@Component
public class CalculatorAspect {

    @Pointcut("execution(public int com.zzj.calculatar.service.CalculatorService.*(..))")
    public void pointCut(){
        
    }
    
    //前置增强:目标方法执行之前先调用增强方法
    @Before("pointCut()")
    public void before(JoinPoint jp){
        //获取参数
        Object [] args = jp.getArgs();    
        //获取方法名
        Signature signature = jp.getSignature();
        String name = signature.getName();
        
        System.out.println("The "+ name +" method begins.");
        System.out.println("Parameters of the "+name+" method["+args[0]+","+args[1]+"]");
    }
    
    //后置增强:目标方法执行之后调用增强方法
    @After("pointCut()")
    public void after(JoinPoint jp){
        //获取方法名
        Signature signature = jp.getSignature();
        String name = signature.getName();
        System.out.println("The "+ name +" method ends.");
    }
    
    //返回增强:目标方法执行return之后返回结果之前调用增强方法,如果出异常则不执行
    @AfterReturning(value="pointCut()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result){
        //获取方法名
        Signature signature = jp.getSignature();
        String name = signature.getName();
        System.out.println("The "+ name +" method results:"+result);
    }
    
    //异常增强:目标方法执行产生异常调用增强方法
    @AfterThrowing(value="pointCut()",throwing = "e")
    public void afterReturning(JoinPoint jp,Exception e){
        //获取方法名
        Signature signature = jp.getSignature();
        String name = signature.getName();
        System.out.println("The "+ name +" method exception:"+e);
    }
}

测试类:

public class Test {

    public static void main(String[] args){
        
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
        System.out.println(calculatorService.getClass());//当有代理类时,获取的代理对象
        System.out.println(calculatorService.div(1,1));
        applicationContext.close();
        
    }
    
}

测试结果:

 当把除法的分母1改为0出现异常时,测试结果:

 环绕增强,包含前面四种增强

@Order(1)//切片执行顺序,默认为字典顺序
@Aspect
@Component
public class CalculatorAspect {

    @Pointcut("execution(public int com.zzj.calculatar.service.CalculatorService.*(..))")
    public void pointCut(){
        
    }
    //环绕增强
    @Around(value="pointCut()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;
        Object target = joinPoint.getTarget();//目标对象
        String methodName = joinPoint.getSignature().getName();
        Object[] params = joinPoint.getArgs();
        
        try{
            try{
                //前置增强
                System.out.println(target.getClass().getName()+": The "+methodName+" method begins.");
                System.out.println(target.getClass().getName()+": Parameters of the "+methodName+"method: ["+params[0]+","+params[1]+"]");
                //执行目标对象内的方法
                result = joinPoint.proceed();
            }finally{
                //后置增强
                System.out.println(target.getClass().getName()+":The "+methodName+" method ends.");
            }
            //返回增强
            System.out.println(target.getClass().getName()+":Result of the "+methodName+" method:"+result);
        }catch (Throwable e) {
            System.out.println(target.getClass().getName()+":Exception of the method "+methodName+": "+e);
        }
        return result;
    }
    
}

测试类:

public class Test {

    public static void main(String[] args){
        
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
        System.out.println(calculatorService.getClass());//当有代理类时,获取的代理对象
        System.out.println(calculatorService.div(1,1));
        applicationContext.close();
        
    }
    
}

测试结果:

当把除法分母1改为0,报错时测试结果如下:

原文地址:https://www.cnblogs.com/yimengxianzhi/p/12261963.html