学习目标:基于XML的AOP开发
一、切点表达式配置
- 切点表达式的写法:
常用第三个表达式
<aop:before method="before" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:before>
<aop:after method="After" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:after>
- 通知类型
具体实现:
- 环绕通知
public Object Around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前。。。。");
//切点
Object obj= pjp.proceed();
System.out.println("环绕后。。。。");
return obj;
}
//配置语句:
<aop:around method="Around" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:around>
- 异常抛出通知
通知类文件代码
public void afterThrowing(){
System.out.println("异常抛出。。。");
}
//配置文件代码:
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:after-throwing>
二、切点表达式的抽取
主要是提高代码的效率
具体代码如下:
在配置文件中修改
<!-- 抽取切点表达式-->
<aop:pointcut id="myPcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:around method="Around" pointcut-ref="myPcut"></aop:around>