有没有一种方法可以监视方法,以便在代码的其他地方执行另一个方法?

huangapple go评论70阅读模式
英文:

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

问题

有人可以帮助我并解释如何最好地观察一个方法。这意味着当执行某个特定方法时,整个系统应该在另一个点内部重新计算,例如。例如,当上传文件时,后台应该触发某些计算。

观察者(Observer)已被弃用,属性更改监听器(PropertyChangeListener)在我的情况下不起作用。

有人有想法或模式如何在Java中映射这种情况吗?

谢谢

英文:

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

得分: 2

在我看来,有两种方法可以实现:
1)使用用户AspectJ。就像这两个例子:12

2)创建您自己的注解。然后,您可以接收要作为参数触发的方法。类似于这样的形式:

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

    }
英文:

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(){
    
     }
    

huangapple
  • 本文由 发表于 2020年8月3日 17:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63227142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定