Exemple d’annotation Spring AOP + AspectJ

Exemple d'annotation Spring AOP + AspectJ

Dans ce didacticiel, nous vous montrons comment intégrer l'annotation AspectJ au framework Spring AOP. En simple, Spring AOP + AspectJ vous permet d'intercepter facilement la méthode.

Annotations AspectJ courantes:

  1. @Before - Exécuter avant l'exécution de la méthode

  2. @After - Exécuter après que la méthode a renvoyé un résultat

  3. @AfterReturning - Exécuté après que la méthode a renvoyé un résultat, intercepte également le résultat renvoyé.

  4. @AfterThrowing - Exécute après que la méthode lève une exception

  5. @Around - Faites le tour de l'exécution de la méthode, combinez les trois conseils ci-dessus.

Note
Pour Spring AOP sans prise en charge d'AspectJ, lisez cecibuild-in Spring AOP examples.

1. Structure du répertoire

Voir la structure du répertoire de cet exemple.

directory structure of this example

2. Dépendances du projet

Pour activer AspectJ, vous avez besoin deaspectjrt.jar,aspectjweaver.jar etspring-aop.jar. Voir le fichier Mavenpom.xmluivant.

AspectJ supported since Spring 2.0
Cet exemple utilise Spring 3, mais les fonctionnalités AspectJ sont prises en charge depuis Spring 2.0.

Fichier: pom.xml



    
        3.0.5.RELEASE
    

    

        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
        
            org.springframework
            spring-aop
            ${spring.version}
        

        
            org.aspectj
            aspectjrt
            1.6.11
        

        
            org.aspectj
            aspectjweaver
            1.6.11
        

    

3. Haricots de printemps

Le bean normal, avec peu de méthodes, l'intercepte plus tard via l'annotation AspectJ.

package com.example.customer.bo;

public interface CustomerBo {

    void addCustomer();

    String addCustomerReturnValue();

    void addCustomerThrowException() throws Exception;

    void addCustomerAround(String name);
}
package com.example.customer.bo.impl;

import com.example.customer.bo.CustomerBo;

public class CustomerBoImpl implements CustomerBo {

    public void addCustomer(){
        System.out.println("addCustomer() is running ");
    }

    public String addCustomerReturnValue(){
        System.out.println("addCustomerReturnValue() is running ");
        return "abc";
    }

    public void addCustomerThrowException() throws Exception {
        System.out.println("addCustomerThrowException() is running ");
        throw new Exception("Generic Error");
    }

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}

4. Activer AspectJ

Dans le fichier de configuration Spring, mettez «<aop:aspectj-autoproxy />», et définissez votre Aspect (intercepteur) et le bean normal.

Fichier: Spring-Customer.xml



    

    

    
    

4. AspectJ @Before

Dans l'exemple ci-dessous, la méthodelogBefore() sera exécutée avant l'exécution de l'interface customerBo, méthodeaddCustomer().

Note
AspectJ «pointcuts» est utilisé pour déclarer quelle méthode va intercepter, et vous devriez vous référer à ceSpring AOP pointcuts guide pour la liste complète des expressions de pointcuts prises en charge.

Fichier: LoggingAspect.java

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) {

        System.out.println("logBefore() is running!");
        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");
    }

}

Exécuter

    CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
    customer.addCustomer();

Sortie

logBefore() is running!
hijacked : addCustomer
******
addCustomer() is running

5. AspectJ @After

Dans l'exemple ci-dessous, la méthodelogAfter() sera exécutée après l'exécution de l'interface customerBo, méthodeaddCustomer().

Fichier: LoggingAspect.java

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) {

        System.out.println("logAfter() is running!");
        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");

    }

}

Exécuter

    CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
    customer.addCustomer();

Sortie

addCustomer() is running
logAfter() is running!
hijacked : addCustomer
******

6. AspectJ @AfterReturning

Dans l'exemple ci-dessous, la méthodelogAfterReturning() sera exécutée après l'exécution de l'interface customerBo, méthodeaddCustomerReturnValue(). De plus, vous pouvez intercepter la valeur renvoyée avec l'attribut «returning».

Pour intercepter la valeur renvoyée, la valeur de l'attribut "return" (résultat) doit être identique au paramètre de méthode (résultat).

Fichier: LoggingAspect.java

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) {

    System.out.println("logAfterReturning() is running!");
    System.out.println("hijacked : " + joinPoint.getSignature().getName());
    System.out.println("Method returned value is : " + result);
    System.out.println("******");

   }

}

Exécuter

    CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
    customer.addCustomerReturnValue();

Sortie

addCustomerReturnValue() is running
logAfterReturning() is running!
hijacked : addCustomerReturnValue
Method returned value is : abc
******

7. AspectJ @AfterReturning

Dans l'exemple ci-dessous, la méthodelogAfterThrowing() sera exécutée si l'interface customerBo, la méthodeaddCustomerThrowException() lève une exception.

Fichier: LoggingAspect.java

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) {

    System.out.println("logAfterThrowing() is running!");
    System.out.println("hijacked : " + joinPoint.getSignature().getName());
    System.out.println("Exception : " + error);
    System.out.println("******");

    }
}

Exécuter

    CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
    customer.addCustomerThrowException();

Sortie

addCustomerThrowException() is running
logAfterThrowing() is running!
hijacked : addCustomerThrowException
Exception : java.lang.Exception: Generic Error
******
Exception in thread "main" java.lang.Exception: Generic Error
    //...

8. AspectJ @Around

Dans l'exemple ci-dessous, la méthodelogAround() sera exécutée avant l'interface customerBo, la méthodeaddCustomerAround(), et vous devez définir le «joinPoint.proceed();» pour contrôler quand l'intercepteur doit renvoyer le contrôle au méthode originaleaddCustomerAround().

Fichier: LoggingAspect.java

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 {

    System.out.println("logAround() is running!");
    System.out.println("hijacked method : " + joinPoint.getSignature().getName());
    System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));

    System.out.println("Around before is running!");
    joinPoint.proceed(); //continue on the intercepted method
    System.out.println("Around after is running!");

    System.out.println("******");

   }

}

Exécuter

    CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
    customer.addCustomerAround("example");

Sortie

logAround() is running!
hijacked method : addCustomerAround
hijacked arguments : [example]
Around before is running!
addCustomerAround() is running, args : example
Around after is running!
******

Conclusion

Il est toujours recommandé d'appliquer l'annotation AsjectJ la moins puissante. C'est un article assez long sur AspectJ au printemps. pour plus d'explications et d'exemples, veuillez visiter les liens de référence ci-dessous.

Anti annotation or using JDK 1.4 ?
Pas de souci, AspectJ a également pris en charge la configuration XML, lisez ceciSpring AOP + AspectJ XML example.

Télécharger le code source

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