且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring中基于注解@AspectJ的AOP实现

更新时间:2022-09-07 12:35:47

Spring中基于注解@AspectJ的AOP实现Spring中基于注解@AspectJ的AOP实现

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

@Aspect

public class Logging {

  /** Following is the definition for a pointcut to select

   *  all the methods available. So advice will be called

   *  for all the methods.

   */

  @Pointcut("execution(* com.sap.*.*(..))")

  private void selectAll(){}

  /**  

   * This is the method which I would like to execute

   * before a selected method execution.

   */

  @Before("selectAll()")

  public void beforeAdvice(){

     System.out.println("Going to setup student profile.");

  }

  /**  

   * This is the method which I would like to execute

   * after a selected method execution.

   */

  @After("selectAll()")

  public void afterAdvice(){

     System.out.println("Student profile has been setup.");

  }

  /**  

   * This is the method which I would like to execute

   * when any method returns.

   */

  @AfterReturning(pointcut = "selectAll()", returning="retVal")

  public void afterReturningAdvice(Object retVal){

     System.out.println("Returning:" + retVal.toString() );

  }

  /**

   * This is the method which I would like to execute

   * if there is an exception raised by any method.

   */

  @AfterThrowing(pointcut = "selectAll()", throwing = "ex")

  public void AfterThrowingAdvice(IllegalArgumentException ex){

     System.out.println("There has been an exception: " + ex.toString());    

  }  

}Spring中基于注解@AspectJ的AOP实现Spring中基于注解@AspectJ的AOP实现Spring中基于注解@AspectJ的AOP实现