Spring-AOP的通知类型

前置通知

  • 在目标方法执行之前进行操作
  • 也就是在目标方法执行之前执行你指定的方法,可以在目标方法执行之前你可以做一些操作
<bean id="goodsDao" class="top.it6666.dao.impl.GoodsDaoImpl"/>
<bean id="myAspect" class="top.it6666.aspect.MyAspect"/>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* top.it6666.dao.impl.GoodsDaoImpl.save(..))"/>

    <aop:aspect ref="myAspect">
        <aop:before method="checkPrivilege" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

后置通知

  • 在目标方法执行之后进行操作
  • 修改 MyAspect 加入一下内容:
/**
 * @author: BNTang
 **/
public class MyAspect {
    public void writeLog(){
        System.out.println("record log");
    }
}
  • 修改 applicationContext.xml
<bean id="goodsDao" class="top.it6666.dao.impl.GoodsDaoImpl"/>
<bean id="myAspect" class="top.it6666.aspect.MyAspect"/>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* top.it6666.dao.impl.GoodsDaoImpl.save(..))"/>

    <aop:aspect ref="myAspect">
        <aop:after-returning method="writeLog" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

  • 测试类代码在之前的文章当中 自行查看

环绕通知

  • 在目标方法执行之前和之后进行操作
  • 在目标方法执行之前和之后做什么操作
  • 修改 MyAspect,修改之后的内容如下:
/**
 * @author: BNTang
 **/
public class MyAspect {
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("before method");

        Object proceed = joinPoint.proceed();

        System.out.println("after method");

        return proceed;
    }
}
  • 修改 applicationContext.xml
<bean id="goodsDao" class="top.it6666.dao.impl.GoodsDaoImpl"/>
<bean id="myAspect" class="top.it6666.aspect.MyAspect"/>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* top.it6666.dao.impl.GoodsDaoImpl.save(..))"/>

    <aop:aspect ref="myAspect">
        <aop:around method="around" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

异常抛出通知

  • 程序出现异常时进行操作
  • 程序出现了异常要做什么操作
  • 修改 MyAspect,修改过后内容如下:
/**
 * @author: BNTang
 **/
public class MyAspect {
    public void exceptionNotice(Throwable throwable){
        System.out.println("happen exception = " + throwable.getMessage());
    }
}
  • 修改 applicationContext.xml
<bean id="goodsDao" class="top.it6666.dao.impl.GoodsDaoImpl"/>
<bean id="myAspect" class="top.it6666.aspect.MyAspect"/>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* top.it6666.dao.impl.GoodsDaoImpl.save(..))"/>

    <aop:aspect ref="myAspect">
        <aop:after-throwing method="exceptionNotice" pointcut-ref="pointcut" throwing="throwable"/>
    </aop:aspect>
</aop:config>

  • 修改 GoodsDaoImpl
/**
 * @author: BNTang
 **/
public class GoodsDaoImpl implements GoodsDao {
    @Override
    public void save() {
        System.out.println(1 / 0);

        System.out.println("save goods");
    }
}
  • 测试类代码还是之前的,这里只是为了演示这个异常抛出通知特意抛出的

最终通知

  • 无论代码是否有异常,都会执行
  • 也就是目标方法执行之后,在执行的过程当中不管有没有异常都会执行的方法,也就是要做的操作
  • 修改 MyAspect,修改之后的内容如下:
/**
 * @author: BNTang
 **/
public class MyAspect {
    public void end(){
        System.out.println("end inform");
    }
}
  • 修改 applicationContext.xml
<bean id="goodsDao" class="top.it6666.dao.impl.GoodsDaoImpl"/>
<bean id="myAspect" class="top.it6666.aspect.MyAspect"/>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* top.it6666.dao.impl.GoodsDaoImpl.save(..))"/>

    <aop:aspect ref="myAspect">
        <aop:after method="end" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

posted @   BNTang  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示