课时9::基于Schema形式的AOP实现

.1)基于Schema形式的AOP实现

  1.导入相应jar包 前置通知示例已经有了 这里就不做过多的讲解

  2.编写通知类该类不需要继承或者写任何注解

package net.bdqn.hbz.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 通知类
 */
public class LogSchema {

    /**
     * 前置通知
     */
    public void before(){
        System.out.println("基于Schema进行了前置通知");
    }

    /**
     * 后置通知
     * @param jp 目标信息
     * @param returnValue 返回值
     */
    public void after(JoinPoint jp,Object returnValue){
        System.out.println("基于Schema进行了前置通知");
    }

    public void execution(Throwable throwable){
        System.out.println("基于Schema异常通知:"+throwable.getMessage());
    }

    /**
     * 环绕通知
     */
    public void huanrao(ProceedingJoinPoint point){
        System.out.println("基于Schema执行了环绕前置通知");
        try{
            point.proceed();
            System.out.println("基于Schema执行环绕后置通知");
            System.out.println("环绕基于Schema目标方法名称:"+point.getSignature().getName());
        }catch (Throwable throwable){
            System.out.println("基于Schema执行了环绕异常通知");
        }finally {
            System.out.println("基于Schema执行了环绕最终通知");
        }
    }
}

  3.编写配置

<!--    注入通知类-->
    <bean id="logSchema" class="net.bdqn.hbz.aop.LogSchema"/>
<!--    建立关联-->
    <aop:config>
<!--        目标信息-->
        <aop:pointcut id="tra" expression="execution(public void addStudent(net.bdqn.hbz.pojo.Student))"/>
<!--        建立连接-->
        <aop:aspect ref="logSchema">
<!--            将目标对象添加前置通知-->
            <aop:before method="before" pointcut-ref="tra"></aop:before>
 <!--            将目标对象添加后置通知-->
            <aop:after-returning returning="returnValue" method="after" pointcut-ref="tra"></aop:after-returning>
<!--          将目标对象添加异常通知  -->
            <aop:after-throwing method="execution" throwing="throwable" pointcut-ref="tra"></aop:after-throwing>
<!--            将目标对象添加环绕-->
            <aop:around method="huanrao" pointcut-ref="tra"></aop:around>
        </aop:aspect>
    </aop:config>

    3.1 ref="logSchema"代表的是通知bean的id值

    3.2 method=“值” 代表的是通知类的方法

    3.3 pointcut-ref=“值” 代表目标信息

    3.4 returning=“值” 返回值 throwing=“值” 捕获的异常范围

 

猜你喜欢

转载自www.cnblogs.com/thisHBZ/p/12511984.html