Spring boot uses Aop's Demo

package com.complat.admin.conf ;
 import lombok.extern.slf4j.Slf4j ;
 import org.aspectj.lang.ProceedingJoinPoint ;
 import org.aspectj.lang.annotation .* ;
 import org.aspectj.lang.JoinPoint ;
 import org. springframework.stereotype.Component ;
 import java.util.Arrays ;
 @Aspect //Describe an aspect class, this annotation needs to be marked when defining an aspect class
 @Component @
 Slf4j
 public class SpringAop {




    @Pointcut("execution(* com.complat.admin.controller.*.*(..))")
    public void pointcut(){
        System.out.println ( "Create a pointcut " ) ;
 }    

    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        System.out.println("這是前置通知");
    }
    @After("pointcut()")
    public void after(JoinPoint joinPoint){
        System.out.println ( "This is a post notification " ) ;
 }    

    @AfterReturning("pointcut()")
    public void afterReturning(JoinPoint joinPoint){System.out.println( "This is a post-return" ) ;
 System.out.println ( 
        Arrays.toString ( joinPoint.getArgs ()) )
 ; }            

    @AfterThrowing("pointcut()")
    public void afterThrowing(JoinPoint joinPoint){
        System.out.println ( "Post- Exception " ) ;
     }

    @Around("pointcut()")
    public void around(ProceedingJoinPoint poin) throws Throwable {
        System.out .println ( "surround notification" ) ;
 log .info( "surround notification" ) ;
 poin.proceed () ;
 }                    
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325523492&siteId=291194637