首页 > 代码库 > Spring AOP 使用总结

Spring AOP 使用总结

spring AOP 使用总结

第一种形式:基于注解(推荐)
@Aspect
@Component
public class MyAdvice{

  @Before(value="http://www.mamicode.com/execution(* xxx.xxx.*(..))")
  public void before() {
    System.out.println("前置增强......");
  }

  @AfterReturning(value="http://www.mamicode.com/execution(* xxx.xxx.*(..))")
  public void after() {
    System.out.println("后置增强......");
  }

  @Around(value="http://www.mamicode.com/execution(* xxx.xxx.*(..))")
  public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

    System.out.println("方法之前.....");
    Object retVal = proceedingJoinPoint.proceed();//执行目标方法
    System.out.println("方法之后.....");
    return retVal;
  }

  @AfterThrowing(value="http://www.mamicode.com/execution(* xxx.xxx.*(..))",throwing="ex")
  public void throwingException(XXXException ex){
    System.out.println("抛出XXX异常时执行.....");
  }

  @After(value="http://www.mamicode.com/execution(* xxx.xxx.*(..))")
  public void doRelease() {
    //不管方法是正常结束还是抛出异常都会执行,类似于finally,主要用于释放资源
    System.out.println("finally doRelease.....");
  }

}
applicationContext.xml配置
<!-- 开启aop操作 -->
<aop:aspectj-autoproxy/>
<!-- 开启注解 -->
<context:component-scan base-package="xxx.xxx.xxx"/>

------------------------------------------------------------------------------------
第二种形式
public class MyAdvice{
  public void before() {
    System.out.println("前置增强......");
  }

  public void after() {
    System.out.println("后置增强......");
  }

  //环绕增强
  public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    //方法之前
    System.out.println("方法之前.....");

    //执行被增强的方法
    Object ret = proceedingJoinPoint.proceed();

    //方法之后
    System.out.println("方法之后.....");

    return ret;
  }
  //异常增强
  public void throwingException(){
    System.out.println("异常抛出时执行.....");
  }
}

applicationContext.xml配置

<!-- 1 配置Advice对象 -->
<bean id="myAdvice" class="xxx.xxx.MyAdvice"></bean>

<!-- 2 配置aop操作 -->
<aop:config>
  <!-- 2.1 配置切入点 -->
  <aop:pointcut expression="execution(* xxx.xxx.*.*(..))" id="pc"/>
  <!-- 2.2 配置切面 -->
  <aop:aspect ref="myAdvice">
    <aop:before method="before" pointcut-ref="pc"/>
    <aop:after-returning method="after" pointcut-ref="pc"/>
    <aop:around method="around" pointcut-ref="pc"/>
    <aop:after-throwing method="throwingException" pointcut-ref="pc"/>
  </aop:aspect>
</aop:config>
----------------------------------------------------------------------------------------
第三种形式:实现接口
1》前置增强功能
public class BeforeAdvice implements MethodBeforeAdvice{
  /**
  * 在目标方法之前执行
  * method:目标方法对象
  * args:方法的参数
  * target:被代理的目标对象
  */
  @Override
  public void before(Method method, Object[] args, Object target)throws Throwable {

  }
}
2》后置增强功能
public class AfterAdvice implements AfterReturningAdvice{
  //在目标方法之后执行
  @Override
  public void afterReturning(Object ret, Method method, Object[] args, Object target) throws Throwable {

  }
}
3》环绕增强功能
public class AroundAdvice implements MethodInterceptor{
  //methodInvocation:目标方法对象
  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    System.out.println("方法之前");

    Object retVal = methodInvocation.proceed();//调用目标方法

    System.out.println("方法之后");

    return retVal;
  }
}
4》异常时的增强功能(了解)
public class ExceptionAdvice implements ThrowsAdvice {
  //目标中抛出异常时执行
  //ex:抛出的异常对象
  @Override
  public void afterThrowing(Exception ex){

  }
}

applicationContext.xml配置

<!-- 1 配置Advice对象 -->
<bean id="beforeAdvice" class="xxx.xxx.BeforeAdvice"></bean>
<bean id="afterAdvice" class="xxx.xxx.AfterAdvice"></bean>
<bean id="aroundAdvice" class="xxx.xxx.AroundAdvice"></bean>
<bean id="exceptionAdvice" class="xxx.xxx.ExceptionAdvice"></bean>

<!-- 2 配置aop操作 -->
<aop:config>
  <!-- 2.1 配置切入点 -->
  <aop:pointcut expression="execution(* xxx.xxx.*.*(..))" id="pc"/>
  <!-- 2.2 配置切面 -->
  <aop:advisor advice-ref="beforeAdvice" pointcut-ref="pc"/>
  <aop:advisor advice-ref="afterAdvice" pointcut-ref="pc"/>
  <aop:advisor advice-ref="aroundAdvice" pointcut-ref="pc"/>
  <aop:advisor advice-ref="exceptionAdvice" pointcut-ref="pc"/>
</aop:config>

Spring AOP 使用总结