Spring AOP AspectJ dans l’exemple de configuration XML

Spring AOP + AspectJ dans l'exemple de configuration XML

Dans ce didacticiel, nous vous montrons comment convertir les derniersSpring AOP + AspectJ annotation en configuration XML.

Pour ceux qui n'aiment pas les annotations ou qui n'utilisent pas JDK 1.4, vous pouvez utiliser AspectJ en XML à la place.

Revoyez la dernière interface customerBo, avec quelques méthodes, vous apprendrez plus tard comment l'intercepter via AspectJ dans un fichier XML.

package com.example.customer.bo;

public interface CustomerBo {

    void addCustomer();

    String addCustomerReturnValue();

    void addCustomerThrowException() throws Exception;

    void addCustomerAround(String name);
}

1. AspectJ = @Before

Exemple 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) {
        //...
    }

}

Fonctionnalité équivalente en XML, avec<aop:before>.





  

     
     

     

  

2. AspectJ = @After

Exemple 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) {
        //...
    }

}

Fonctionnalité équivalente en XML, avec<aop:after>.





  

     
     

     

  

3. AspectJ = @AfterReturning

Exemple 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) {
    //...
   }

}

Fonctionnalité équivalente en XML, avec<aop:after-returning>.





  

    
    

    

  

4. AspectJ = @AfterReturning

Exemple 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) {
    //...
  }
}

Fonctionnalité équivalente en XML, avec<aop:after-throwing>.





  

    
    

    

  

5. AspectJ = @Around

Exemple 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 {
        //...
    }

}

Fonctionnalité équivalente en XML, avec<aop:after-around>.





   

    
   

   

  

Exemple XML complet

Voir le fichier de configuration complet basé sur AspectJ XML.












  

    
    

    

    
    

    

    
    

    

    
    

    

    
    

    

  



Télécharger le code source

Téléchargez-le -Spring3-AOP-AspectJ-XML-Example.zip (8 Ko)