79453307

Date: 2025-02-20 03:59:19
Score: 2.5
Natty:
Report link

I'm not sure if I've misunderstood, but if I have, please let me know.

Are you trying to execute the save method multiple times in method A, but the save method is already listened to by AOP, so you want to execute the rest of the AOP process after all the save methods in method A are finished?

Based on my understanding, my solution is shown below

@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();
    }
}

..

@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;
}

..

public void processBatch() {
    // test entity list
    List<News> newsList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        News tempNews = new News();
        tempNews.setTitle("this is a temp entity");
        newsList.add(tempNews);
    }

    // handle
    System.out.println("START, pause AOP");
    batchProcessingState.enableBatchMode();  // close AOP

    try {
        for (News news : newsList) {
            save(news);  // AOP logic does not trigger
        }
    } finally {
        batchProcessingState.disableBatchMode();  // resumption AOP
        System.out.println("FINISH, resumption AOP");
    }
}

Rusult:

START, pause AOP

(5 times the save method was executed)

FINISH, resumption AOP
Reasons:
  • Whitelisted phrase (-1): solution is
  • RegEx Blacklisted phrase (2.5): please let me know
  • RegEx Blacklisted phrase (1): I have, please
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: LaiFQZzr