我们可以在 Spring AOP 中使用参数调用 joinpoint.proceed

Can we call joinpoint.proceed with arguments in Spring AOP

我正在尝试使用批量参数调用 joinpoint.proceed。 可以打电话吗

我找不到任何示例,其中我们正在划分参数或创建新参数,然后对它们调用 joinpoint.proceed。

这是您要找的吗?

@Service
public class AdditionService {
    public Integer sum(List<Integer> list) {
        Integer sum = 0;
        for (Integer i : list) {
            sum += i;
        }
        System.out.println("Sum :" + sum);
        return sum;
    }
}

以及批量求和的一个方面

@Aspect
@Component
public class ExampleAspect {
    @Around("execution(* com.package..*.sum*(..)) && within(com.package..*) && args(list)")
    public Integer around(ProceedingJoinPoint pjp, List<Integer> list) throws Throwable {
        Object[] args = pjp.getArgs(); // get the arguments array
        Integer sum = 0;
        for (int i = 0; i < 10; i += 5) {
            args[0] = (list.subList(i, i + 5)); // modify the arguments array
            System.out.println(args[0]);
            sum += (Integer) pjp.proceed(args);
        }
        return sum;
    }
}

如下访问的服务bean

Integer[] a= {1,2,3,4,5,6,7,8,9,10};
Integer sum = 0;
sum = service.sum(Arrays.asList(a));
System.out.println("Total : "+sum);

会将以下内容打印到控制台

[1, 2, 3, 4, 5]
Sum :15
[6, 7, 8, 9, 10]
Sum :40
Total : 55

希望对您有所帮助