Spring框架(五)

17 篇文章 0 订阅
11 篇文章 0 订阅

上一篇是对AOP的入门,这一篇就进一步细化。

Spring中的通知

1.前置通知:在执行目标方法前(权限校验)

  • 可以显示连接点信息
public class MyAspect {
    public void Inform(JoinPoint joinPoint){
        System.out.println("before infrormation,joinpoint"+joinPoint);
    }
}

在这里插入图片描述
2.后置通知:在执行目标方法后

  • 可以获得返回值
<aop:config>
        <!--切点配置 *代表任意返回值,..代表任意参数--!>
        <aop:pointcut id="pointcut02" expression="execution(* AOP.PersonDaoimpl.add(..))"/>
        <!--切面配置-->
        <aop:aspect ref="MyAspect">
            <aop:after-returning method="Log" pointcut-ref="pointcut02" returning="result"/>
        </aop:aspect>
    </aop:config>
package AOP;
import org.aspectj.lang.JoinPoint;
public class MyAspect {
    public void Inform(JoinPoint joinPoint){
        System.out.println("before infrormation\t\tjoinpoint:"+joinPoint);
    }
    public void Log(Object result){
        System.out.println("操作已记录");
        System.out.println("日志=========="+result);
    }
}

在这里插入图片描述
3.环绕通知:前置+后置

 <aop:config>
        <!--切点配置 *代表任意返回值,..代表任意参数-->
        <aop:pointcut id="pointcut03" expression="execution(* AOP.PersonDaoimpl.delete())"/>
        <!--切面配置-->
        <aop:aspect ref="MyAspect">
            <aop:around method="around" pointcut-ref="pointcut03"/>
        </aop:aspect>
    </aop:config>
//在前后中间要执行切入点,所以必须传入切入点参数
  public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("before around");
        Object o = joinPoint.proceed();
        System.out.println("after around");
    }

4.抛出异常通知:程序出异常时

<aop:config>
        <!--切点配置 *代表任意返回值,..代表任意参数--!>
        <aop:pointcut id="pointcut04" expression="execution(* AOP.PersonDaoimpl.update(..))"/>
        <!--切面配置-->
        <aop:aspect ref="MyAspect">
            <aop:after-throwing method="Throw" pointcut-ref="pointcut04"/>
        </aop:aspect>
    </aop:config>
 /***
     * 异常抛出通知
     */
    public void Throw(){
        System.err.println("Throw Exception!");//异常输出
    }
 @Override
    public void update() {
        int i = 100/0; //在实现类写一个异常
        System.out.println("Update");

    }

在这里插入图片描述

  • 显示异常信息
  /***
     * 异常抛出通知,异常类型的ex必须和xml中配置的相同
     */
    public void Throw(Throwable ex){
        System.err.println("Throw Exception!");
        System.out.println(ex);
    }
 <aop:after-throwing method="Throw" pointcut-ref="pointcut04" throwing="ex"/>

在这里插入图片描述
5.最终通知:无论代码有无异常,总会被执行的。(相当于finally)

   <aop:after method="after" pointcut-ref="pointcut04"/>

    /**
     * 最终通知
     * */
    public void after(){

        System.out.println("最终通知");
    }

在这里插入图片描述

切入点表达式语法(基于execution函数)

语法:[访问修饰符] 返回值 包名.类名.方法名(参数) *可以放在任意位置,除过参数用…表示任意.访问符可省略.
一下是几个示例

  • public void AOP.PersonDaoimp.delete()       具体的方法
  • * AOP.*     AOP包下的所有方法
  • * AOP.PersonDao+.delete()     PersonDao类和它所有子类的delete方法
  • * AOP….(…)         AOP包和它子包的所有类的所有方法

下边举一个例子
把execution函数里写为AOP包下的所有方法执行前置通知并输出切入点

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="PersonDao1" class="AOP.PersonDaoimpl"></bean>
    <bean id="MyAspect" class="AOP.MyAspect"></bean>
    <aop:config>
        <!--切点配置 *代表任意返回值,..代表任意参数-->
        <aop:pointcut id="pointcut01" expression="execution(* AOP.*.*(..))" ></aop:pointcut>
        <!--切面配置-->
        <aop:aspect ref="MyAspect">
            <aop:before method="Inform" pointcut-ref="pointcut01"></aop:before> 
        </aop:aspect>
    </aop:config>
</beans>

输出了所有前置通知和各自的切入点
在这里插入图片描述
这些表达式必须包裹在execution函数内.
xml配置AOP的内容就写到这里,下一篇讲讲注解配置AOP




精彩下次继续!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值