开发者

Aspectj and catching private or inner methods

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 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() ?

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>


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...");
 }

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜