Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring AOP

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have the following situation

Some main program calls the method doSomeOperation which in turn calls the other methods in the class based on business rule. I have a situation where I have to populate some stats after some of the methods in this class is invoked.

For example, after calling doSomeOperation which in turn calls say doOps1, populate some stats table on the database indicating how many records were inserted/updated/deleted etc in specific table and how much time it took etc by doOps1 method. I am trying to use Spring AOP for this purpose. However the issue that I am facing is that the intended code is not getting invoked.



The Spring bean definition



On running the main program, I am getting the output (and it is not calling profile1 rom the Aspect class - BusinessProfiler). however if I directly call the doOps1 from main class then the aspect method gets invoked. I would like to know if aspect is supposed to work if only called from the main method and not otherwise.




 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that by default Spring uses interface based proxies. Meaning to call the method through the proxy you must do it through the interface. You have a self call that is not done through the proxy but on a reference of this


You could try proxying the target class by adding that option to your <aop:aspectj-autoproxy /> tag



or you could also look at different weaving options. There is a blog I found that talks about this a little bit

http://denis-zhdanov.blogspot.com/2009/07/spring-aop-top-problem-1-aspects-are.html

The Spring reference documentation also has a lot of material on proxy choices.
 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This did not solve the problem. I am reading the documentation to understand the finer details.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had my doubts the proxy target class would work, but you did not experiment with other weaving options.

Now if you expose doOps1 via the interface and access it through that interface it will work

i.e.



If you want your original code to work as you posted it originally with the self call, then weave in your aspects at compile time, then it will work. This can be done with the maven plugin see the below POM fragment for an example.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic