Spring - AOP基于XML(aop:aspect)

前面演示了AOP基于注解实现,为了与代码解耦,这次使用XML配置方式。

【1】XML配置


    
    
    <bean id="arithmeticCalculator" class="com.web.aop.impl.xml.ArithmeticCalculatorImpl">bean>

    
    <bean id="loggingAspect" class="com.web.aop.impl.xml.LoggingAspect">bean>

    <bean id="validateAspect" class="com.web.aop.impl.xml.ValidateAspect">bean>

    
    <aop:config>
        
        <aop:pointcut id="pointcut" expression="execution(* com.web.aop.impl.xml.ArithmeticCalculatorImpl.*(int, int))" />

        
        <aop:aspect  ref="loggingAspect" order="1" >
            <aop:before method="beforMethod" pointcut-ref="pointcut"/>
            <aop:after method="afterMethod" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"/>

        aop:aspect>

        
        <aop:aspect  ref="validateAspect" order="2" >
            <aop:before method="beforMethod" pointcut-ref="pointcut"/>
        aop:aspect>

    aop:config>

【2】测试代码如下

    • 只是去掉了注解,其他与前面一致。
public class LoggingAspect {
    public void beforMethod(JoinPoint joinPoint){
        /*获取方法名*/
        String methodName = joinPoint.getSignature().getName();
        /*获取方法的参数*/
        List args = Arrays.asList(joinPoint.getArgs());

        System.out.println("The method "+methodName+" begins with"+args);
    }

    /*表示该包下所有返回类型的所有类的所有方法*/
    public void afterMethod(JoinPoint joinPoint){
        /*获取方法名*/
        String methodName = joinPoint.getSignature().getName();
        /*获取方法的参数*/
        List args = Arrays.asList(joinPoint.getArgs());


        System.out.println("The method "+methodName+" ends with"+args);

    }
    /*表示该包下所有返回类型的所有类的所有方法*/
    public void afterReturning(JoinPoint joinPoint ,Object result){
        /*获取方法名*/
        String methodName = joinPoint.getSignature().getName();
        /*获取方法的参数*/
        List args = Arrays.asList(joinPoint.getArgs());


        System.out.println("The method "+methodName+" ends with "+result);

    }

    /*表示该包下所有返回类型的所有类的所有方法*/
    public void afterThrowing(JoinPoint joinPoint ,Exception exception){
        /*获取方法名*/
        String methodName = joinPoint.getSignature().getName();
        /*获取方法的参数*/
        List args = Arrays.asList(joinPoint.getArgs());


        System.out.println("The method "+methodName+" throws exception :"+exception);

    }

    /*
     * 环绕通知需要携带ProceedingJoinPoint类型的参数;
     * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法;
     * 环绕通知必须有返回值,返回值即为目标方法的返回值 !
     * 
     * */

//  /*表示该包下所有返回类型的所有类的所有方法*/
//  public Object aroundMethod(ProceedingJoinPoint joinPoint){
//      
//      System.out.println("This is aroundMethod....");
//      
//      /*获取方法名*/
//      String methodName = joinPoint.getSignature().getName();
//      /*获取方法的参数*/
//      List args = Arrays.asList(joinPoint.getArgs());
//      
//      Object result = null;
//      try {
//          //前置通知
//          System.out.println("The method "+methodName+" begins with  :"+args);
//          
//          result = joinPoint.proceed();
//          
//          //返回通知
//          System.out.println();
//          System.out.println("The method "+methodName+" ends with  :"+result);
//      } catch (Throwable e) {
//          // 异常通知
//          System.out.println("The method "+methodName+" throws exception :"+e);
//          throw new RuntimeException(e);
//      }
//      
//      //后置通知
//      System.out.println("The method "+methodName+" ends ");
//      
//      return result;
//      
//  }

}
 
  

【3】result

The method add begins with[1, 2]
*******The methodadd is validated !!*********
The method add ends with[1, 2]
The method add ends with 3
result------>3
The method mul begins with[1, 2]
*******The methodmul is validated !!*********
The method mul ends with[1, 2]
The method mul ends with 2
result------>2
The method div begins with[10, 0]
*******The methoddiv is validated !!*********
The method div ends with[10, 0]
The method div throws exception :java.lang.ArithmeticException: / by zero

你可能感兴趣的:(Spring)