基于配置xml的AOP入门实现步骤
2020-04-02 07:51
1086 查看
1、先回忆一下基于注解的AOP步骤:
- 将目标类和切面类都加入到ioc容器中。@Component
- 告诉Spring哪个是切面类。@Aspect
- 在切面类中使用五个通知注解来配置切面中的这些通知方法都何时何地运行
- 开启基于注解的AOP功能
2、基于配置xml的AOP步骤:
//接口 package com.atguigu.inter; public interface Calculator { public int add(int a, int b); public int sub(int a, int b); public int mul(int a, int b); public int div(int a, int b); }
//实现类 package com.atguigu.impl; import com.atguigu.inter.Calculator; import org.springframework.stereotype.Service; @Service public class MyMathCalculator implements Calculator { @Override public int add(int a, int b) { return a+b; } @Override public int sub(int a, int b) { return a-b; } @Override public int mul(int a, int b) { return a*b; } @Override public int div(int a, int b) { return a/b; } }
//第一个切面类 package com.atguigu.utils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.util.Arrays; public class ValidateAspect { public static void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数 Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("[validateAspect]["+ name +"]" + "方法开始执行了,用的参数列表是:["+ Arrays.asList(args) +"]"); } public static void logReturn(JoinPoint joinPoint, Object result){ Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("[validateAspect]["+ name +"]" + "方法正常执行完成了,计算结果是:"+result); } public static void logException(JoinPoint joinPoint, Exception e){ System.out.println("[validateAspect]["+ joinPoint.getSignature().getName() +"]"+ "出现了异常,异常原因是:"+"["+e+"]"); } public static void logEnd(JoinPoint joinPoint){ System.out.println("[validateAspect]["+ joinPoint.getSignature().getName() +"]" + "方法执行结束了。"); } }
//第二个切面类 package com.atguigu.utils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; public class LogUtils { public static void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数 Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("【LogUtils】["+ name +"]" + "方法开始执行了,用的参数列表是:["+ Arrays.asList(args) +"]"); } public static void logReturn(JoinPoint joinPoint, Object result){ Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数 Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("【LogUtils】["+ name +"]" + "方法正常执行完成了,计算结果是:"+result); } public static void logException(JoinPoint joinPoint, Exception e){ Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数 Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("【LogUtils】["+ name +"]"+ "出现了异常,异常原因是:"+"["+e+"]"); } public static void logEnd(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数 Signature signature = joinPoint.getSignature();//获取到方法签名 String name = signature.getName(); System.out.println("【LogUtils】["+ name +"]" + "方法执行结束了。"); } public Object myArround(ProceedingJoinPoint pjp) throws Throwable { //获取目标方法运行时使用的参数 Object[] args = pjp.getArgs(); String name = pjp.getSignature().getName(); Object proceed = null; try { System.out.println("【LogUtils】[环绕前置通知],"+name+"方法开始"); //利用反射调用目标方法,相当于method.invoke(obj, args); proceed = pjp.proceed(args); System.out.println("【LogUtils】[环绕返回通知],"+name+"方法返回,返回值:"+ proceed); } catch (Exception e) { System.out.println("【LogUtils】[环绕异常通知],异常信息:"+e); } finally { System.out.println("【LogUtils】[环绕后置通知],"+name+"方法最终结束。"); } //反射调用后的返回值一定返回出去 return proceed; } }
- 注意下面的配置文件,xsi:schemaLocation的配置信息,不能缺少。
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--基于配置xml的AOP步骤--> <bean id="myMathCalculator" class="com.atguigu.impl.MyMathCalculator"></bean> <bean id="logUtils" class="com.atguigu.utils.LogUtils"></bean> <bean id="validateAspect" class="com.atguigu.utils.ValidateAspect"></bean> <!--1、需要AOP名称空间--> <aop:config> <aop:pointcut id="globalPoint" expression="execution(* com.atguigu.impl.*.*(..))"/> <!--2、指定切面--> <aop:aspect ref="logUtils" order="1"> <!--3、配置切入点表达式--> <aop:pointcut id="mypoint" expression="execution(* com.atguigu.impl.*.*(..))"/> <!--4、配置哪些方法是通知,有些通知还有返回注意--> <aop:before method="logStart" pointcut="execution(* com.atguigu.impl.*.*(..))"></aop:before> <aop:after-returning method="logReturn" pointcut-ref="mypoint" returning="result" ></aop:after-returning> <aop:after-throwing method="logException" pointcut-ref="mypoint" throwing="e"></aop:after-throwing> <aop:after method="logEnd" pointcut-ref="mypoint"></aop:after> <aop:around method="myArround" pointcut-ref="mypoint"></aop:around> </aop:aspect> <aop:aspect ref="validateAspect" order="2"> <aop:before method="logStart" pointcut-ref="globalPoint"></aop:before> <aop:after-returning method="logReturn" pointcut-ref="globalPoint" returning="result"></aop:after-returning> <aop:after-throwing method="logException" pointcut-ref="globalPoint" throwing="e"></aop:after-throwing> <aop:after method="logEnd" pointcut-ref="globalPoint"></aop:after> </aop:aspect> </aop:config> </beans>
- 执行结果,可以看下每个切面方法的顺序。在LogUtils.java文件里面写了环绕通知,该类也是先于validateAspect先执行。
- 注意:这三个包也是必需的。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring—基于XML方式配置AOP实现通知
- Spring AOP之---基于ProxyFactory的类编码方式和XML配置方式实现AOP
- Spring框架学习(3.1)基于xml配置文件的AOP实现
- 基于XML配置以及基于注解开发AspectJ实现AOP
- 使用Spring框架入门三:基于XML配置的AOP的使用
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
- Spring AOP(3)基于XML配置实现的示例
- Spring实现AOP方式之一:基于XML配置的Spring AOP
- spring基于xml配置AOP,的4种常用 切入点表达式的 步骤写法
- 基于xml配置的spring aop 的实现日志 和为通知传递参数
- Aop(基于XML配置文件实现)
- Spring 基于XML配置的AOP入门案例
- 基于AOP实现Ibatis的缓存配置过期策略
- Spring AOP AspectJ注解和XML配置两种实现(Maven构建)
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- Spring3.0 入门进阶(三):基于XML方式的AOP使用
- Struts2基于XML配置文件实现权限校验
- Spring AOP - 基于XML配置文件
- Struts2学习笔记(十四):基于XML配置方式实现对action的所有方法进行校验
- spring aop的使用(注解方式以及基于xml配置方式)