Aspectj和捕获私有或内部方法 [英] Aspectj and catching private or inner methods

查看:1057
本文介绍了Aspectj和捕获私有或内部方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring配置AspectJ,并且在捕获从类外调用的公共方法时它工作正常。现在我想做这样的事情:

I've configureg AspectJ with Spring and it works fine when "catching" public methods called from out of the class. Now I want do something like this:

public class SomeLogic(){

   public boolean someMethod(boolean test){

      if(test){
        return innerA();
      } else {
        return innerB();
      }
   }


   private boolean innerA() {// some logic}
   private boolean innerA() {// some other logic}

}

SomeLogic是一个SpringBean。方法innerA()和innerB()可以声明为private或public - 从Struts动作调用someMethod()方法。是否有可能从AspectJ中捕获从someMethod()调用的方法innerA()或innerB()?

SomeLogic is a SpringBean. The methods innerA() and innerB() could be declared as private or public - the method someMethod() is called from a Struts action. Is it possible to catch with AspectJ the methods innerA() or innerB() called from someMethod() ?

我的配置(基于XML):

My config (XML based):

    <aop:aspect id="innerAAspect" ref="INNER_A">
        <aop:pointcut id="innerAService" expression="execution(* some.package.SomeLogic.innerA(..))"/>
    </aop:aspect>

    <aop:aspect id="innerAAround" ref="INNER_A">
        <aop:around pointcut-ref="innerAService" method="proceed"/>
    </aop:aspect>


    <aop:aspect id="innerBAspect" ref="INNER_B">
        <aop:pointcut id="innerBService" expression="execution(* some.package.SomeLogic.innerB(..))"/>
    </aop:aspect>

    <aop:aspect id="innerBAround" ref="INNER_B">
        <aop:around pointcut-ref="innerBService" method="proceed"/>
    </aop:aspect>


推荐答案

是的,使用AspectJ很容易捕获私有方法。

Yes it is easy to catch private methods with AspectJ.

在所有私有方法之前打印句子的示例:

An example that prints a sentence before all private methods:

 @Pointcut("execution(private * *(..))")
 public void anyPrivateMethod() {}

 @Before("anyPrivateMethod()")
 public void beforePrivateMethod(JoinPoint jp) {
     System.out.println("Before a private method...");
 }

如果您熟悉Eclipse,我建议使用 STS 或仅安装 AJDT插件

If you are familiar with Eclipse, I recommend to develop AspectJ with STS or only install the AJDT plugin.

有关Spring AOP功能的更多信息,请参阅Spring参考文档这里

More information about Spring AOP capabilities can be found in the Spring reference documentation here.

这篇关于Aspectj和捕获私有或内部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆