@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明aspects 的一种风格。通过在基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。
#
大致流程如下:
(1) 首先利用注解声明一个Aspect:
import org.aspectj.lang.annotation.Aspect;@Aspectpublic class AspectModule {}
该aspect在Beans.xml中需要进行如下配置:
Beans.xml:
(2) 声明一个切入点PointCut. 切入点表达式定义了我们感兴趣的哪个方法会真正被执行。
一个例子:该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:
import org.aspectj.lang.annotation.Pointcut;@Pointcut("execution(* com.tutorialspoint.Student.getName(..))") private void getname() {}
(3) 声明Advice:
@Before("businessService()")public void doBeforeTask(){ ...}@After("businessService()")public void doAfterTask(){ ...}@AfterReturning(pointcut = "businessService()", returning="retVal")public void doAfterReturnningTask(Object retVal){ // you can intercept retVal here. ...}@AfterThrowing(pointcut = "businessService()", throwing="ex")public void doAfterThrowingTask(Exception ex){ // you can intercept thrown exception here. ...}@Around("businessService()")public void doAroundTask(){ ...}
看个具体的例子。
Logging.java:
import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Around;@Aspectpublic class Logging { /** Following is the definition for a pointcut to select * all the methods available. So advice will be called * for all the methods. */ @Pointcut("execution(* com.sap.*.*(..))") private void selectAll(){} /** * This is the method which I would like to execute * before a selected method execution. */ @Before("selectAll()") public void beforeAdvice(){ System.out.println("Going to setup student profile."); } /** * This is the method which I would like to execute * after a selected method execution. */ @After("selectAll()") public void afterAdvice(){ System.out.println("Student profile has been setup."); } /** * This is the method which I would like to execute * when any method returns. */ @AfterReturning(pointcut = "selectAll()", returning="retVal") public void afterReturningAdvice(Object retVal){ System.out.println("Returning:" + retVal.toString() ); } /** * This is the method which I would like to execute * if there is an exception raised by any method. */ @AfterThrowing(pointcut = "selectAll()", throwing = "ex") public void AfterThrowingAdvice(IllegalArgumentException ex){ System.out.println("There has been an exception: " + ex.toString()); } }
定义一个名叫selectAll的Pointcut,execution表达式的值为:execution(* com.sap.*.*(..))
意思是选中package com.sap下的所有Java类方法来动态调用advice.
Beans.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
如果不使用@AspectJ和@pointcut注解,我们需要在Beans.xml里进行冗长的定义aspect和pointcut定义: