Note that there are some explanatory texts on larger screens.

plurals
  1. POAspectJ and calling another method in @AfterReturning method
    primarykey
    data
    text
    <p>I'm starting to learn how to use AspectJ in Spring. I have the following class. I want to perform first method in new thread (long running task), so I thought this might be achieved using Aspect - when firstMethod is called, Aspect automatically delegate this calling into new Thread. After firstMethod is finished, secondMethod should be called with returned by firstMethod value as an argument.</p> <pre><code>public class SimpleClass{ public Object firstMethod(){ //some operations return object; } public void secondMethod(Object returnedObjectByFirstMethod){ } } @Aspect public class SimpleAspect { @Around("execution(* SimpleClass.firstMethod(..))") public void doInNewThread(final ProceedingJoinPoint joinPoint) throws Throwable { Thread t = new Thread(new Runnable() { public void run() { joinPoint.proceed(); } }); t.start(); } @AfterReturning( pointcut = "execution(* SimpleClass.firstMethod(..))", returning= "result") public void doAfter(JoinPoint joinPoint, Object result) { SimpleClass.secondMethod(result); } } </code></pre> <p>This is kind of prototype, I haven't implemeneted it yet, I'm pretty sure it cannot work that way, but I want to make sure about two trivial things:</p> <ol> <li><p>How can I call secondMethod() in doAfter(), if this method would not be static? Do I need kind of an instance of SimpleClass is SimpleAspect? How to supply such instance?</p></li> <li><p>Nearly the same as first question, but what if secondMethod() would not be in the same class as firstMethod()?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload