Frage

I'am building a Rest server with aspectj. For synchronization i want to use aspects. I defined such pointcut to capture all the points where update and delete events happen: I defined an annotation and use the annotation to capture methods to sync:

synchronized pointcut syncJoinPoints():call (@Synchronizes * *(..));

What will happen if the pointcut is synchronized, what does it mean to have synchronized pointcut. Does the thread that intercepts the pointcut is newly created by AspectJ plugin or it uses the thread that is entering the intercepted method?

Thank you for your help.

War es hilfreich?

Lösung

AspectJ does not create threads by itself: the weaving "only" modifies the code by injecting some additional instructions, but it keeps on running in the same context.

The synchronized keyword on the pointcut definition does not do anything useful. If what you want to achieve is to synchronize all calls (or executions, which would mean less modified code) to methods annotated with @Synchronizes on the same lock, you need an around advice:

public aspect SynchronizingAspect {
    private static final Object lock = new Object();

    pointcut syncJointPoint(): execution(@Synchronizes * *.*(..)); // or call()

    Object around(): syncJointPoint() {
        synchronized(lock) {
            return proceed();
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top