spring学习笔记(20)——基于配置文件方式配置AOP

来源:互联网 发布:使命召唤二战 知乎 编辑:程序博客网 时间:2024/04/30 02:59

基于注解的方式配置AOP

查看先前的笔记
- spring学习笔记(15)——AOP基础
- spring学习笔记(16)——AOP之前后置通知
- spring学习笔记(17)——返回通知&异常通知&环绕通知
- spring学习笔记(18)——切面的优先级
- spring学习笔记(19)——重用切点表达式

基于配置文件方式配置AOP

功能代码

package com.zj.asceptj.xml;public class CalcultorImpl implements Calcultor{    @Override    public int add(int a, int b) {        int result = a + b;        return result;    }    @Override    public int sub(int a, int b) {        int result = a - b;        return result;    }}

切面

package com.zj.asceptj.xml;import java.util.Arrays;import java.util.List;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;import org.springframework.stereotype.Component;public class LoggingAscept {    public void beforeMethod(JoinPoint joinpoint){        //注意,JoinPoint来自org.aspectj.lang.JoinPoint,小心导错包        //方法名        String methodName = joinpoint.getSignature().getName();        //方法参数        List<Object> args = Arrays.asList(joinpoint.getArgs());        System.out.println("method "+methodName+" begin:"+args);    }    public void afterMethod(JoinPoint joinpoint){        //注意,JoinPoint来自org.aspectj.lang.JoinPoint,小心导错包        //方法名        String methodName = joinpoint.getSignature().getName();        //方法参数        List<Object> args = Arrays.asList(joinpoint.getArgs());        System.out.println("method "+methodName+" end:"+args);    }    public void afterReturning(JoinPoint joinPoint,Object result){        //方法名        String methodName = joinPoint.getSignature().getName();        System.out.println("method "+methodName+" end:"+result);    }    public void afterThrowing(JoinPoint joinPoint,Exception ex){        //方法名        String methodName = joinPoint.getSignature().getName();        System.out.println("method "+methodName+" occurs:"+ex);    }    public Object around(ProceedingJoinPoint pjd){        Object result = null;        String methodName = pjd.getSignature().getName();        try {            //前置通知            System.out.println("method:"+methodName+" begins with "+Arrays.asList(pjd.getArgs()));            //执行目标方法            result = pjd.proceed();            //返回通知            System.out.println("method:"+methodName+" end with "+result);        } catch (Throwable e) {            // 异常通知            System.out.println("method:"+methodName+" occurs exception "+e);        }        //后置通知        System.out.println("method ends");        return result;    }}

配置文件

<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- 配置bean -->    <bean id="calcultorImpl" class="com.zj.asceptj.xml.CalcultorImpl"></bean>    <bean id="loggingAscept" class="com.zj.asceptj.xml.LoggingAscept"></bean>    <!-- 配置AOP -->    <aop:config>        <!-- 配置切点表达式 -->        <aop:pointcut expression="execution(* com.zj.asceptj.xml.*.*(..))" id="pointcut"/>        <!-- 配置切面以及通知 -->        <aop:aspect ref="loggingAscept" order="1">            <!-- 前置通知 -->            <aop:before method="beforeMethod" pointcut-ref="pointcut"/>            <!-- 后置通知 -->            <aop:after method="afterMethod" pointcut-ref="pointcut"/>            <!-- 返回通知 -->            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>            <!-- 异常通知 -->            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>            <!-- 环绕通知 -->            <!--             <aop:around method="around" pointcut-ref="pointcut"/>             -->        </aop:aspect>        <!-- 可以配置多个切面 -->    </aop:config></beans>
0 0