如何暂停/恢复一些弹簧aoppointcut

问题描述 投票:0回答:1

在一个春季应用程序中,我们添加了一些AOP尖端来进行拦截,... JPA存储库的方法在每次插入实体时,更新实体时,都可以刷新含蓄的视图。

,但是,我们有一些批处理过程,需要大量时间调用thoses
save
方法。
(不,由于现有代码太复杂而无法重构时,不可能在批处理结束时制作独特的saveall)

有一种简单的方法,要求春季在批处理处理过程中暂停特定的AOP尖端,然后在需要时恢复它?

saveAll

我不确定我是否误会了,但是如果我有,请告诉我。

您试图在
save
中多次执行
java spring aop spring-aop
1个回答
0
投票
方法,但是AOP已经聆听了

save方法,因此您要在所有method A

方法之后执行AOP过程的其余部分。

save

完成了吗?
基于我的理解,我的解决方案如下所示。
save
..
method A
..

@Component public class BatchProcessingState { private final AtomicBoolean isBatchMode = new AtomicBoolean(false); public void enableBatchMode() { isBatchMode.set(true); } public void disableBatchMode() { isBatchMode.set(false); } public boolean isBatchMode() { return isBatchMode.get(); } }

rusult:

@Autowired
private BatchProcessingState batchProcessingState;

@Around("xxx")
public Object aroundSaveMethods(ProceedingJoinPoint joinPoint) throws Throwable {
    if (batchProcessingState.isBatchMode()) {
        return joinPoint.proceed();
    }

    System.out.println("Intercepts the save method and executes AOP logic...");
    Object result = joinPoint.proceed();
    System.out.println("The save method completes");
    return result;
}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.