@aspect注解_Spring中基于注解@AspectJ的AOP实现

@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.

da8f639d40634f9f96dc872effec55af

Beans.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>

如果不使用@AspectJ和@pointcut注解,我们需要在Beans.xml里进行冗长的定义aspect和pointcut定义:

0e1f680f342c49c696eb0846722f57b1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值