Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
270 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...