Aspect-Oriented Programming (AOP) in springboot

1. What is AOP

AOP is to dynamically create proxy objects for certain classes in the project during the running process, and complete additional functions in the objects to ensure that they can focus more on their own business logic development when processing business.
There are the following concepts:
1. Advice (Advice) - adding additional operations to the target method is called advice, and can also be called enhancement
2. Joinpoint (Joinpoint) - is the place that can be enhanced (somewhere before and after the method Phase)
3. Pointcut - the actual enhancement place
4. Aspect - the class that encapsulates the notification and pointcut for horizontal insertion
5. Proxy - apply the notification to the target class dynamically Created object
6. Weaving (Weaving) - the process of inserting the facet into the target to generate a proxy object

2.springboot uses aop

insert image description here
insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
Section class code:

@Component
@Aspect //切面类注解
@Order(2)
public class AsceptTest {
    
    

    @Pointcut("execution(* com.study.service.UserService.eat(*))")
    private void Myeat(){
    
    };


    //前置通知。value匹配规则,也就是指定拦截的切入点(方法)
//    @Before(value = "execution(* com.study.service.UserService.eat(*))") // execution匹配某些方法效率,whithin匹配某些类(中的所有方法)
    @Before(value = "Myeat()")
    public void before(JoinPoint joinPoint){
    
    
        System.out.println("当前方法名: "+joinPoint.getSignature().getName());
        System.out.println("当前执行方法参数: "+joinPoint.getArgs()[0]);
        System.out.println("目标对象target: "+joinPoint.getTarget());
        System.out.println("前置增强——保姆做饭");
    }

    //后置通知
//    @After(value = "execution(* com.study.service.UserService.eat(*))")
    @After("Myeat()")
    public void after(JoinPoint joinPoint){
    
    
        System.out.println("当前方法名: "+joinPoint.getSignature().getName());
        System.out.println("当前执行方法参数: "+joinPoint.getArgs()[0]);
        System.out.println("目标对象target: "+joinPoint.getTarget());
        System.out.println("后置增强——保姆洗碗");
    }


    //环绕通知
    @Around(value = "execution(* com.study.service.UserService.out())" )
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
    
     //环绕增强必须要获取ProceedingJoinPoint参数,
        System.out.println("当前方法名: "+joinPoint.getSignature().getName());
//        System.out.println("当前执行方法参数: "+joinPoint.getArgs()[0]); 目标对象没参数不可获取
        System.out.println("目标对象target: "+joinPoint.getTarget());
        System.out.println("环绕增强——保安开门"); //目标方法执行前
        Object proceed = joinPoint.proceed(); //执行目标方法
        System.out.println("环绕增强——保安关门"); //目标方法执行后
        return proceed; //返回目标方法执行结果
    }

    //异常通知
    @AfterThrowing(value = "within(com.study.service.UserService+)",throwing="e")
    public void ex(JoinPoint joinPoint,Exception e){
    
    
        System.out.println("出错了"+e.getMessage());
    }

    //返回通知,方法正常返回则生效
    @AfterReturning(value = "within(com.study.service.UserService+)",returning="result")
    public void ex(JoinPoint joinPoint,Object result){
    
    
        System.out.println(joinPoint.getSignature().getName()+ ":返回通知生效");
        System.out.println("返回了"+result);
    }


}

Guess you like

Origin blog.csdn.net/worilb/article/details/114156126