AOP-AspectJ-基于XML的实现——各种通知

来源:互联网 发布:js给集合添加元素 编辑:程序博客网 时间:2024/05/29 17:31

AOP-AspectJ-基于XML的实现——各种通知

Spring AOP-AspectJ-基于XML的实现各种通知

切面类

package com.hk.spring.xml;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** *  定义一个简单的POJO类作为切面类 * @author 浪丶荡 * @param <throwing> * *///切面类public class MyAspect<throwing> {    /**     *  前置通知     */    public void before(){        System.out.println("前置方法");    }    /**     *  前置通知2     */    public void before(JoinPoint j){        System.out.println("前置方法"+j);    }    /**     *  后置通知     */    public void afterReturing(){        System.out.println("后置方法执行 ");    }    /**     *  后置通知2     */    public void afterReturing(String result){        System.out.println("后置方法执行   result="+result);    }    /**     *  环绕通知     */    public String around(ProceedingJoinPoint p){        Object result = null;        System.out.println("目标方法执行前");        try {            //执行目标方法            result = p.proceed();        } catch (Throwable e) {            e.printStackTrace();        }        System.out.println("目标方法执行后");        return ((String) result).toLowerCase();    }    /**     * 异常通知     */    public void afterThrowing(Exception ex){        System.out.println("afterThrowing执行,异常是:"+ex.getMessage());    }    /**     * 异常通知2     */    public void afterThrowing(){        System.out.println("afterThrowing执行,异常是:");    }    /**     * 最终通知     */    public void after(){        System.out.println("最终通知方法");    }}

配置文件

<?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="someService" class="com.hk.spring.xml.ISomeServiceImpl"></bean>        <!-- 注册切面 -->        <bean id="myAspect" class="com.hk.spring.xml.MyAspect"></bean>        <!-- aop配置 -->        <aop:config>            <!-- 切入点表达式 -->            <aop:pointcut expression="execution(* *..ISomeService.doFirst(..))" id="doFirstPointcut"/>            <aop:pointcut expression="execution(* *..ISomeservice.doSecond(..))" id="doSecondPointcut"/>            <aop:pointcut expression="execution(* *..ISomeservice.doThird(..))" id="doThirdPointcut"/>            <!-- 切面 -->            <aop:aspect ref="myAspect">                <!-- 前置通知 pointcut-ref指定织入点-->                <aop:before method="before" pointcut-ref="doFirstPointcut"/>                <!-- 指定通知(有参数) pointcut-ref指定织入点-->                <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/>                <!-- 最终通知 -->                <aop:after method="after" pointcut-ref="doFirstPointcut"/>                <!-- 后置通知 含参 -->                <aop:after-returning method="afterReturing(java.lang.String)" pointcut-ref="doFirstPointcut" returning="result"/>                <!-- 后置通知 -->                <aop:after-returning method="afterReturing" pointcut-ref="doFirstPointcut"/>                <!-- 环绕通知 不需要加参数org.aspectj.lang.ProceedingJoinPoint因为自身已经包含了-->                <aop:around method="around" pointcut-ref="doThirdPointcut"/>                 <!-- 异常通知 -->                <aop:after-throwing method="afterThrowing" pointcut-ref="doSecondPointcut"/>                <!-- 异常通知含参 -->                <aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="doSecondPointcut" throwing="ex"/>            </aop:aspect>        </aop:config></beans>
0 0
原创粉丝点击