AOP——基于Schema

本文介绍如何在 Spring 中使用 AOP 进行切面编程,包括切面、切入点和通知的基本配置方法,并探讨了四种不同的切面定义方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 声明一个切面

 切面使用<aop:aspect/>来声明

 

<aop:config>
  <aop:aspect id="myAspect" ref="aBean">
    ...
  </aop:aspect>
</aop:config>

<bean id="aBean" class="..."/>

 

  • 声明一个切入点
<aop:config>
  <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="logService" expression="execution(* com.pms.service.*.*(..))"/>
    <aop:before pointcut-ref="logService" method="log"/>
  </aop:aspect>
</aop:config>

<bean id="aBean" class="..."/>

 

 

  • 声明通知

 

//前置通知
<aop:before pointcut-ref="..." method="..."/>

//后置通知
<aop:after-returing pointcut-ref="..." method="..." returing="..."/>

//异常通知
<aop:after-throwing pointcut-ref="..." method="..." throwing="..."/>

//最终通知
<aop:after pointcut-ref="..." method="..."/>

//环绕通知
<aop:around pointcut-ref="..." method="..."/>

 

  •  定义切面的4种方式

1.基于@AspectJ注解的方式

2.基于<aop:aspect>的方式

3.基于<aop:advisor>的方式

4.基于Advisor类的方式

 

  • Spring AOP中使用@AspectJ还是XML?

Java5以下只能选择XML方式,XML风格有个缺点,不能完全将需求实现的地方封装到一个位置。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值