0

I have a class that is using Spring AOP framework in my web app just like the code shown below. I was wondering why the Spring AOP was able to trace add() but not able trace multiple() if I implement the following code.

public interface calculator {
   public void add();
   public void multiple();
}

public class calculatorImpl implements calculator {

   public void add() {

      multiple();
   }

   public void multiple() {}
}

I've did an experiment and found out that the following code is working fine. Meaning that the Spring AOP able to trace both add and multiple function.

ICalculator calcProxy = (ICalculator) context.getBean("calculatorProxy");
calcProxy.add();
calcProxy.multiple();

I think that must be cause by the multiple() was inject by the proxy bean whereas the multiple() in calculatorImpl class wasn't, thus Spring AOP wasn't able to trace it. Correct me if I am wrong.

My next question. Is there a work around on this issue where the add() really need to execute multiple() and get trace by the Spring AOP?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
huahsin68
  • 6,819
  • 20
  • 79
  • 113

1 Answers1

2

Spring AOP doesn't change the actual method, but adds a proxy around the Object. Once you are inside the inner Object (inside the add() method), there is no proxy, you are underneath it.

I have explained this mechanism in more detail in this past answer:

https://stackoverflow.com/a/4171649/342852

There are three ways to deal with that situation:

  1. Use AspectJ, not Spring AOP. AspectJ compiles the behavior into your code, whereas Spring AOP works with proxies that surround your code.
  2. Access the Proxy from inside the Bean. This is reeeeaaally ugly, but it can be done.
    Example code (here you make the class aware of it's being proxied, which is contrary to everything AOP stands for):

    public void add() {
       ((Calculator)AopContext.currentProxy()).multiple();
    }
    

    Slightly less horrible (here you make the class aware that it's being managed by Spring):

    @Autowired
    private ApplicationContext context;
    public void add() {
       context.getBean(Calculator.class).multiple();
    }
    
  3. Change your design. Example: move the two methods to separate beans and inject each into the other bean.
Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588