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

查看:54
本文介绍了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 - 方法someMethod() 是从Struts 操作中调用的.是否可以使用 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 开发 AspectJ 或仅安装 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天全站免登陆