Spring AOP示例

本文介绍如何在Spring AOP中通过@Order注解设置切面的执行优先级,值越小优先级越高。同时,展示了如何通过@Pointcut声明切点表达式并重用,简化切面的维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

切面的优先级
为项目增加一个新的切面类,负责验证功能,则需要指定切面执行的顺序。即切面的优先级。具体方法是给切面类增加@Order注解,并指定具体的数字,值越小优先级越高
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.springframework.core.annotation.Order;
9 import org.springframework.stereotype.Component;
10
11 /**
12 * 可以使用@Order注解指定切面的优先级,值越小优先级越高
13 * @author yul
14 *
15 */
16 @Order(2)
17 @Component
18 @Aspect
19 public class ValidationAspect {
20
21 @Before("execution(public int com.yl.spring.aop.ArithmeticCalculator.*(..))")
22 public void vlidateArgs(JoinPoint joinPoint) {
23 System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
24 }
25 }

切点表达式的重用:
在LoggingAspect类中,切点的表达式可以先定义,在使用。
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.ProceedingJoinPoint;
7 import org.aspectj.lang.annotation.After;
8 import org.aspectj.lang.annotation.AfterReturning;
9 import org.aspectj.lang.annotation.AfterThrowing;
10 import org.aspectj.lang.annotation.Around;
11 import org.aspectj.lang.annotation.Aspect;
12 import org.aspectj.lang.annotation.Before;
13 import org.aspectj.lang.annotation.Pointcut;
14 import org.springframework.core.annotation.Order;
15 import org.springframework.stereotype.Component;
16 @Order(1)
17 @Component
18 @Aspect
19 public class LoggingAspect {
20
21 /**
22 * 定义一个方法,用于声明切入点表达式。一般的,该方法中再不需要添加其他的代码
23 * 使用@Pointcut 来声明切入点表达式
24 * 后面的其他通知直接使用方法名直接引用方法名即可
25 */
26 @Pointcut("execution(public int com.yl.spring.aop.ArithmeticCalculator.*(..))")
27 public void declareJoinPointExpression() {
28
29 }
30
31 /**
32 * 在com.yl.spring.aop.ArithmeticCalculator接口的每一个实现类的每一个方法开始之前执行一段代码
33 */
34 @Before("declareJoinPointExpression()")
35 public void beforeMethod(JoinPoint joinPoint) {
36 String methodName = joinPoint.getSignature().getName();
37 Object[] args = joinPoint.getArgs();
38 System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
39 }
40
41 /**
42 * 在com.yl.spring.aop.ArithmeticCalculator接口的每一个实现类的每一个方法执行之后执行一段代码
43 * 无论该方法是否出现异常
44 */
45 @After("declareJoinPointExpression()")
46 public void afterMethod(JoinPoint joinPoint) {
47 String methodName = joinPoint.getSignature().getName();
48 Object[] args = joinPoint.getArgs();
49 System.out.println("The method " + methodName + " ends with " + Arrays.asList(args));
50 }
51
52 /**
53 * 方法正常结束后执行的代码
54 * 返回通知是可以访问到方法的返回值的
55 */
56 @AfterReturning(value="declareJoinPointExpression()", returning="result")
57 public void afterReturning(JoinPoint joinPoint, Object result) {
58 String methodName = joinPoint.getSignature().getName();
59 System.out.println("The method " + methodName + " return with " + result);
60 }
61
62 /**
63 * 在方法出现异常时会执行的代码
64 * 可以访问到异常对象,可以指定在出现特定异常时在执行通知代码
65 */
66 @AfterThrowing(value="declareJoinPointExpression()", throwing="ex")
67 public void afterThrowing(JoinPoint joinPoint, Exception ex) {
68 String methodName = joinPoint.getSignature().getName();
69 System.out.println("The method " + methodName + " occurs exception: " + ex);
70 }
71
72 /**
73 * 环绕通知需要携带ProceedingJoinPoint类型的参数
74 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
75 * 而且环绕通知必须有返回值,返回值即为目标方法的返回值
76 */
77 @Around("declareJoinPointExpression()")
78 public Object aroundMethod(ProceedingJoinPoint pjd) {
79 Object result = null;
80 String methodName = pjd.getSignature().getName();
81 //执行目标方法
82 try {
83 //前置通知
84 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
85 result = pjd.proceed();
86 //返回通知
87 System.out.println("The method " + methodName + " ends with " + Arrays.asList(pjd.getArgs()));
88 } catch (Throwable e) {
89 //异常通知
90 System.out.println("The method " + methodName + " occurs expection : " + e);
91 throw new RuntimeException(e);
92 }
93 //后置通知
94 System.out.println("The method " + methodName + " ends");
95 return result;
96 }
97
98 }

当处于不同的类,甚至不同的包时,可以使用包名.类名.方法名
具体代码如下:
1 package com.yl.spring.aop;
2
3 import java.util.Arrays;
4
5 import org.aspectj.lang.JoinPoint;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.aspectj.lang.annotation.Before;
8 import org.springframework.core.annotation.Order;
9 import org.springframework.stereotype.Component;
10
11 /**
12 * 可以使用@Order注解指定切面的优先级,值越小优先级越高
13 * @author yul
14 *
15 */
16 @Order(2)
17 @Component
18 @Aspect
19 public class ValidationAspect {
20
21 @Before("com.yl.spring.aop.LoggingAspect.declareJoinPointExpression()")
22 public void vlidateArgs(JoinPoint joinPoint) {
23 System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
24 }
25 }
内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值