Is there a way to monitor methods so that another method is executed elsewhere in the code?

126 views Asked by At

Can someone help me and explain how best to observe a method. This means that when a certain method is executed, the entire system should be recalculated internally at another point, for example. The same for example when a file is uploaded, then certain calculations should be triggered in the background.

Observer is deprecated and PropertyChangeListener does not help me in my case i think.

Does anyone have an idea or pattern how to map this case in Java?

Thanks

1

There are 1 answers

0
Eduardo Briguenti Vieira On

In my opinion there are 2 ways to do it:

  1. User AspectJ. Like these two exemples: 1, 2

  2. Create you own Annotation. Then you can receive the methods you want to trigger as the parameters. Something like this:

     @MyCustomTriggerAnnotation(methodsToBeTriggered= {methodA, methodB})
     public void test(){
    
     }