学习Spring AOP编程的一些备忘录

xiaoxiao2021-02-28  36

综述:项目需求,补充学习了Spring AOP,练习了小项目,出现的问题记录备忘一下。


备忘列表

导入的Jar包列表以及applicationContext.xml示例约束文件schema添加@Before JoinPoint的jar包各种通知总结多个通知的优先级切入点表达式

导入的Jar包列表以及applicationContext.xml示例

applicationContext.xml示例

<?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:context="http://www.springframework.org/schema/context" 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/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"> <context:component-scan base-package="tqj.springAOP.day_01.b"></context:component-scan> <!-- 使AspectJ 起作用 :自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

约束文件schema添加

约束文件位置:spring-framework-4.0.3.RELEASE-docs\spring-framework-reference\html\xsd-config.html

@Before

@Before("execution(public int tqj.springAOP.day_01.b.MathematicCalculatorImpl.add(int, int))")

主要是强调一下 public int,不加会出现错误。

或者,更通用一些:

@Before("execution(* tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))")

JoinPoint的jar包

写 ` String methodName = joinpoint.getSignature().getName()` 的时候找不到**.getSianature()**方法,发现导入的Jar包不对,应该导入的是以下的Jar包 import org.aspectj.lang.JoinPoint;

各种通知总结

@Before("execution(public int tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))") public void before(JoinPoint joinpoint){ String methodName = joinpoint.getSignature().getName(); List<Object> args = Arrays.asList(joinpoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); } @After("execution(* tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))") public void after(JoinPoint joinpoint){ String methodName = joinpoint.getSignature().getName(); System.out.println("The method " + methodName + " ends."); } @AfterReturning(value="execution(* tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))",returning = "result")//必须要填上参数,否则会出问题 public void afterReturning(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); } @AfterThrowing(value="execution(* tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))",throwing = "ex")//必须要填上参数,否则会出问题 public void afterThrowing(JoinPoint joinPoint, Exception ex){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs " + ex);} //全能的环绕通知@Around(全能不代表最常用) /** * 环绕通知需要携带 ProdeedingJointPoint 类型的参数 * 类似于动态代理的全过程: ProdeedingJointPoint 类型的参数可以决定是否执行目标方法 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 */ @Around("execution(* tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); result = pjd.proceed(); System.out.println("The method ends with " + result); } catch (Throwable e) { System.out.println("The method occurs exception" + e); } System.out.println("The method " + methodName + " ends."); return result; };

多个通知的优先级

使用@Order(数字),括号里的数字越小,优先级越高

切入点表达式

/** * 定义一个方法,用于声明切入点表达式,一般地,该方法中不需要添加其他代码 */ @Pointcut("execution(public int tqj.springAOP.day_01.b.MathematicCalculatorImpl.*(..))") public void declareJoinPointExpression(){} @Before("declareJoinPointion()") //注意这的变化 public void before(JoinPoint joinpoint){ String methodName = joinpoint.getSignature().getName(); List<Object> args = Arrays.asList(joinpoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); } // !!当不在一个包时的用法,需要加上包名和方法 public class ValidationAspect { @Before("tqj.springAOP.day_01.b.LoggingAspect.declareJoinPointExpression()")//注意这的变化,加上了包名 public void validate(){ System.out.println("Validate --> "); } }
转载请注明原文地址: https://www.6miu.com/read-600150.html

最新回复(0)