XML構成のSpring AOP AspectJの例

XML構成のSpring AOP + AspectJの例

このチュートリアルでは、最後のSpring AOP + AspectJ annotationをXMLベースの構成に変換する方法を示します。

アノテーションやJDK 1.4を使用したくない場合は、代わりにXMLでAspectJを使用できます。

いくつかの方法で、最後のcustomerBoインターフェースを再度確認します。後で、XMLファイルのAspectJを介してインターセプトする方法を学習します。

package com.example.customer.bo;

public interface CustomerBo {

    void addCustomer();

    String addCustomerReturnValue();

    void addCustomerThrowException() throws Exception;

    void addCustomerAround(String name);
}

1. AspectJ = @Before

AspectJ @Beforeの例。

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

    @Before("execution(* com.example.customer.bo.CustomerBo.addCustomer(..))")
    public void logBefore(JoinPoint joinPoint) {
        //...
    }

}

<aop:before>を使用したXMLの同等の機能。





  

     
     

     

  

2. AspectJ = @After

AspectJ @Afterの例。

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;

@Aspect
public class LoggingAspect {

    @After("execution(* com.example.customer.bo.CustomerBo.addCustomer(..))")
    public void logAfter(JoinPoint joinPoint) {
        //...
    }

}

<aop:after>を使用したXMLの同等の機能。





  

     
     

     

  

3. AspectJ = @AfterReturning

AspectJ @AfterReturningの例。

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
public class LoggingAspect {

  @AfterReturning(
   pointcut = "execution(* com.example.customer.bo.CustomerBo.addCustomerReturnValue(..))",
   returning= "result")
   public void logAfterReturning(JoinPoint joinPoint, Object result) {
    //...
   }

}

<aop:after-returning>を使用したXMLの同等の機能。





  

    
    

    

  

4. AspectJ = @AfterReturning

AspectJ @AfterReturningの例。

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
public class LoggingAspect {

  @AfterThrowing(
   pointcut = "execution(* com.example.customer.bo.CustomerBo.addCustomerThrowException(..))",
   throwing= "error")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
    //...
  }
}

<aop:after-throwing>を使用したXMLの同等の機能。





  

    
    

    

  

5. AspectJ = @Around

AspectJ @Aroundの例。

package com.example.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;

@Aspect
public class LoggingAspect {

    @Around("execution(* com.example.customer.bo.CustomerBo.addCustomerAround(..))")
    public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        //...
    }

}

<aop:after-around>を使用したXMLの同等の機能。





   

    
   

   

  

完全なXMLの例

完全なAspectJ XMLベースの構成ファイルを参照してください。












  

    
    

    

    
    

    

    
    

    

    
    

    

    
    

    

  



ソースコードをダウンロード

ダウンロード–Spring3-AOP-AspectJ-XML-Example.zip(8 KB)