如何在方面内调用钩子类的方法?

时间:2014-02-06 09:06:06

标签: java hook aop aspectj

也许主题的名字很糟糕。我试着解释清晰度。

我上课了:

public class A(){
   Field1 field1;
   Field2 field2;
   public void method1(){...}
   public void method2(){...}
   public void sourceMethod(ParameterClass parameter1){
     //some code
     method1();
     //some code
      method2();
     //some cdoe
  }
}

我将挂钩源方法: ...

@Around(value = "execution(* A.sourceMethod(ParameterClass))")
    public void aroundSourceMethod(JoinPoint joinPoint){
      //I need to write my realization sourceMethod here
      // I want to invoke method1 and method2 here
}

这里我要重写所有代码。但我需要调用method1()method2()

是否可能使用AspectJ?

1 个答案:

答案 0 :(得分:1)

@Around(value = "execution(* A.sourceMethod(Parameter)) && target(target)")
public void aroundSourceMethod(JoinPoint joinPoint, Object target){
   // I need to write my realization sourceMethod here
   // I want to invoke method1 and method2 here
}

Target将包含执行sourceMethod的对象。由于您只建议A.sourceMethod(),您可以假设它是A类型,将其强制转换为它并根据需要调用其方法:

((A) target).method1()
...

它不漂亮,但它应该有用。